为什么我使用 launchOption 字典 nil 获得 Apple Push Notification?

Why I get Apple Push Notification with launchOption dictionary nil?

我正在从服务器发送推送通知,如果应用程序在后台 运行,通知到达并点击它调用 didReceiveRemoteNotification()。

但是当我终止我的应用程序时(双击主页按钮后向上滑动应用程序,通知到达但点击通知只会将屏幕定向到应用程序主页。我已经在 didFinishLaunchingWithOptions:launchOptions() 中编码,如下所示。

 if (launchOptions != nil)
    {
        NSDictionary* dictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {

            NSLog(@"Launched from push notification: %@", dictionary);

            NSString *strVendId=[dictionary valueForKeyPath:@"payload.vendor_id"];

            UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UINavigationController *navController = (UINavigationController *)self.window.rootViewController;

            VenderDetailController *vDetail=[mainstoryboard instantiateViewControllerWithIdentifier:@"venDetail"];
            vDetail.strVendorId=strVendId;
            [navController pushViewController:vDetail animated:YES];
        }
    }

当应用程序被杀死时,我无法调试它。也许我没有得到我无法分辨的字典。我什至尝试删除 launchOption nil 条件,仍然没有帮助。

我正在使用 objective C,这个问题快把我逼疯了。

试试这个...

NSDictionary *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

首先检查设备日志中的日志以确认Dictionary是否为nil。用于在应用程序终止时检查日志。转到 Window -> 设备,然后 select 您的设备,然后点击左下角的箭头图标,如下图所示。即使您不是 运行 来自 Xcode 的应用程序,您也可以看到所有日志:

记录您正在获取的词典,这将有助于调试。

您可以使用 Chandan005 的解决方案来查看调试控制台,或者您可以使用 didFinishLaunchingWithOptions:launchOptions 中的 UIAlertView 显示 dictionary 内容来检查您是否获得了正确的远程通知负载。