iOS Handle notifications after starting app from not-running state

laarsk picture laarsk · Sep 5, 2012 · Viewed 11.5k times · Source

I've been trying to handle receiving notifications in my app, but its not really working out.

When I use didReceiveLocalNotification:(UILocalNotification *)notification. I can receive and use the notification that is used to enter the app, without any problems

However, this function is only fired when the app is already running (active, inactive, background, and possibly suspended, but I haven't tried that yet).

Now, there is this function didFinishLaunchingWithOptions:(NSDictionary *)launchOptions where you can use [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey] which would return a UILocalNotification.

However, when you launch the app from not-running state, this event is not fired. The LocalNotification then opens the app, but I can not use it in any way.

Now, my question is: How can I make it work, so I can receive and process notifications when starting the app, from a notification, when the app is in not-running state? Is there perhaps something I'm doing wrong here?

Here is a bit of sample code from my app:

First, the didFinishLaunchingWithOptions function, which, unfortunatly does not work. The function [sharedLocalNotificationsInstance processNotification:notification] is never launched...

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

LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {

    // Process the received notification
    [sharedLocalNotificationsInstance processNotification:notification];

    application.applicationIconBadgeNumber = 0;
}

return YES;
}

And a second piece of code: The didReceiveLocalNotification function, which works perfectly: I receive the notification, and [sharedLocalNotificationsInstance processNotification:notification] works perfectly.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

// Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];

// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}

Answer

K S picture K S · Sep 12, 2012

This is how iOS handles local notification. It depends on which state of your app, e.g. active, running in background, or not started yet. The iOS will invoke either didFinishLaunchingWithOptions or didReceiveLocalNotification, or won't touch your app at all.

Please see this article for clarification - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html