Crash when handling remote notification when app not running

爷,独闯天下 提交于 2019-11-28 00:02:48
Prasanna

I got this resolved, and it has nothing to do with view controllers, as I thought.

The issue was in the following lines. I was sending in remoteNotif.userInfo rather than remoteNotif itself. Also, remoteNotif is obviously not of type UILocalNotification. It is a NSDictionary object.

Before

UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

[self handleRemoteNotification:application userInfo:remoteNotif.userInfo];

Should be

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

[self handleRemoteNotification:application userInfo:remoteNotif];

if you close the app which start from xcode debug mode, and when the app start with push notification(closed app) if the your phone connected to mac(still your phone in debug mode with xcode) it will be crash. test this senario with unplugged phone.

vakio

You aren't properly initializing your application when receiving a notification. Change the application:didFinishLaunchingWithOptions: method to this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions {
   // Push required screens into navigation controller

   NSDictionary *notif= [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

   [window addSubview:navigationController.view];
   [window makeKeyAndVisible];

   //Accept push notification when app is not open
   if (notif) {      
       [self handleRemoteNotification:application userInfo:notif];
   }

   return YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!