Crash when handling remote notification when app not running
Asked Answered
E

3

8

I receive a remote notification and according to the type of notification, change navigation controller's view controllers.

It all works fine when the app is in the foreground, or when the app is in the background but not completely closed (from multi-tasking bar).

But, when the app is closed, and receives a remote notification it crashes as soon as it opens. Am I doing wrong with the way I am setting up the ViewControllers?

Here's some code.

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

         UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

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

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

    return YES;
}

-(void) handleRemoteNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo {
    application.applicationIconBadgeNumber = 0;

NSMutableArray *viewControllers = [NSMutableArray array];
    [viewControllers addObject:driverWaitViewController];
    [viewControllers addObject:newJobsViewController];

    [navigationController setViewControllers:viewControllers];
}
Edroi answered 28/11, 2010 at 3:7 Comment(0)
E
15

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];
Edroi answered 28/11, 2010 at 23:59 Comment(0)
A
7

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.

Alidis answered 8/6, 2012 at 6:43 Comment(2)
This solved my problem for now, but why is that?! Makes no sense for meGeography
As u said no sense for me also. but i think some connection here with the device and mac.Alidis
S
2

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;
}
Stripe answered 28/11, 2010 at 10:7 Comment(2)
Thanks for your reply Vakio. The issue something else though. Please see how I solved it.Edroi
Yeah I thought that was weird, but I didn't catch up on it. Sorry.Stripe

© 2022 - 2024 — McMap. All rights reserved.