ios - 应用程序关闭时 didFinishLaunchingWithOptions APNS

标签 ios objective-c xcode apple-push-notifications

我正在尝试在应用关闭时连接通知。我需要从 APNS 获取消息并通过 id 打开所需的文章。问题是消息没有被 APNS 在函数 didFinishLaunchingWithOptions 中读取。不知错在哪里。我正在尝试解析 (NSDictionary *) launchOptions 并提取文章的 id。在函数内部 - didReceiveRemoteNotification 一切正常。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[self settingMenuNotification];
[self restKitConfiguration];

[self registerSettingsAndCategories];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@",[paths objectAtIndex:0]);

if(launchOptions != NULL) {
    NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    [self openNewsDetailNotification:apsInfo];
}

return YES;
}

-(void) openNewsDetailNotification:(NSDictionary*)apsInfo {
if( [apsInfo objectForKey:@"id"] != NULL) { //id - id_article
    NSString *id_article = [apsInfo objectForKey:@"id"];

    NSNumber  *objectId = [NSNumber numberWithInteger: [id_article integerValue]];
    NSLog(@"Получил id %@", id_article);

    // Загружаем статью
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIViewController *root = [window rootViewController];
    UIStoryboard *storyboard = root.storyboard;
    NewsDetailViewController *newsVC = (NewsDetailViewController*)[storyboard instantiateViewControllerWithIdentifier:@"NewsDetailViewController"];

    newsVC.objectId = objectId;

    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    [navController.visibleViewController.navigationController pushViewController:newsVC animated:YES];
}
}

最佳答案

我也遇到了同样的情况。我在其中添加了延迟。使用此代码而不是直接调用该函数。

            [self performSelector:@selector(openNewsDetailNotification:) withObject:apsInfo afterDelay:6.0f];

关于ios - 应用程序关闭时 didFinishLaunchingWithOptions APNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981218/

相关文章:

ios - NumberFormatter 未对 "fr_CH"区域设置使用正确的小数分隔符

iOS:为应用程序准备背景图像

ios - 核心数据 : Insertion into One to Many Relationship

objective-c - 使用 NSUserDefaults 错误保存隐藏按钮状态

ios - App Store 拒绝将应用作为预发布软件

ios - 支持 HTTPS Web 服务器 iOS

javascript - iOS 上的 IFrame 高度问题(移动版 safari)

objective-c - 不确定如何使用 objectAtIndex 执行此操作

ios - 使用 Phonegap 安装 Cordova 插件 - no Cordova.plist

swift - Xcode 9.2 : linker command failed with exit code 1 (use -v to see invocation)