Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILocalNotification - How to handle when App is NOT running?

I have implemented UILocalNotifications in two different apps now. One uses Location Services, which means if it gets killed, it (usually) gets restarted (so it's not as big of an issue). However, in another app, I schedule a UILocalNotification based upon time. In this case, I'm having a major problem.

If I schedule a notification, then the app gets killed, pushed out of memory, phone is turned off then on again, I cannot view older notifications "automatically" when opening the app.

Here is the workflow that works (app running in the background):

  1. Launch app; app schedules notification; close app; app is now running in the background
  2. Receive local notification; ignore it at first; pull the drop down menu from the top (status bar); touch the notification to launch the app
  3. Results: The app appropriately displays the information in the notification.

Here is the workflow that does not work (app no longer running in the background):

  1. Launch app; app schedules notification; close app; app is now running in the background
  2. Manually kill app (to simulate my situation)
  3. Still receive local notification; ignore it at first; pull the drop down menu from the top (status bar); touch the notification to launch the app
  4. Results: The app launches, but the didReceiveLocalNotification method is not called. The user thinks the app doesn't work.
    Note: I can't even manually force the information, because if they have received more than one notification, I can't tell which one they touched on to know which one to display.

Is there any way to know which notification they touched on when the app is not running in the background (and thus does not run the didReceiveLocalNotification method)?

like image 367
thephatp Avatar asked Jan 10 '12 18:01

thephatp


2 Answers

In the 2nd case, the user has quit (or killed) the app, so when the user touches the notification your app would be launched.

This can be handled in the below mentioned appDelegate's method

didFinishLaunchingWithOptions is invoked when an app is launched, so just check if the app was actually launched because the user touched the notification (code below):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) 
    {
        NSLog(@"notification caused app to launch, alert body = %@", notification.alertBody);
        // do what ever action you want to do
        // you could just copy the code from "didReceiveLocalNotification" and paste it here
    }

    return YES;
}
like image 104
user1046037 Avatar answered Sep 24 '22 01:09

user1046037


@Ranjit, this will work when user tap the app icon because is an app delegate method. Get called every time the app finished launching. (Can´t comment because my lack of reputation... ;) )

like image 45
Williams_Martinez Avatar answered Sep 23 '22 01:09

Williams_Martinez