Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Handle notifications after starting app from not-running state

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];
}
like image 992
laarsk Avatar asked Sep 05 '12 07:09

laarsk


2 Answers

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

like image 66
K S Avatar answered Oct 04 '22 15:10

K S


<Matrix-Morpheus-Meme title="WHAT IF I TOLD YOU">

When an application is launched from the "not-running" state because a user tapped on a local notification alert, the application has been started by iOS, not by Xcode, thus IT IS NOT RUNNING UNDER THE DEBUGGER. You cannot breakpoint inside it and neither does NSLog() send anything to the Xcode console. Test with a UIAlertController.

</Matrix-Morpheus-Meme>

like image 30
Gary Avatar answered Oct 04 '22 16:10

Gary