17

iPhone version - 5.1 (9B176)

Below is the series of events during Local Notification where in which didFinishLaunchingWithOptions method is not invoked.

  1. App is running in background.
  2. Got local notification - simple notification no dialog.
  3. Click on the app icon which shows the badge number.

Expected as per Apple documentation:

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s didFinishLaunchingWithOptions method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification

Actual : didFinishLaunchingWithOptions NOT invoked. But applicationWillEnterForeground and applicationDidBecomeActive were invoked.

Sujay
  • 2,510
  • 2
  • 27
  • 47
Sushma Satish
  • 2,363
  • 2
  • 20
  • 23

1 Answers1

23

You are correct. The behavior is inconsistent with the documentation. Putting the documentation aside and focusing on actual behavior; The crux of the matter seems to be this: If your app becomes active by the user interacting with the notification you will receive a pointer to the notification, if the user taps your application icon directly you will not.

To illustrate. If you present an alert style notification and the user taps the action button, or if, as in your case, you present a banner notification and the user taps on that you will receive a pointer to the notification in one of two ways:

If your application was in the Not-Running state:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    UILocalNotification *launchNote = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (launchNote){
        // I recieved a notification while not running

    }
}

If your application is running in any state:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    // I recieved a notification
}

In the case where a user elects to cancel an alert style notification, that notification is gone.

The truly annoying an inconsistent part is that if you present a banner notification and the user taps your icon you seem to have no way of retrieving a reference to the presented notifications in the notification center. i.e. they do not appear in the [[UIApplication sharedApplication] scheduledLocalNotifications] array, presumably because they are no longer scheduled but are now presented.

So in short; The documentation is wrong. And there are other annoying inconsistencies. If this behavior is a problem for you you should file a bug with Apple.

NJones
  • 27,139
  • 8
  • 70
  • 88
  • Nope. didReceiveLocalNotification is not invoked.Application is running in background and not in foreground. The app icon is clicked not the notification message. In this case, as per apple documentation, the expected behaviour is to get didFinishLaunchingWithOptions be invoked .. But neither didFinishLaunchingWithOptions nor didReceiveLocalNotification are invoked. – Sushma Satish Mar 27 '12 at 16:51
  • You are correct the documentation is misleading/wrong. I have edited my answer in response. – NJones Mar 27 '12 at 19:15
  • Thanks NJones. I have filed a bug report with Apple. (Bug report ID: 11136947) – Sushma Satish Mar 28 '12 at 08:57
  • You can serialize the UILocalNotification using NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice]; – Rafael Sanches Apr 28 '12 at 19:05
  • + for addressing the "crux of the matter"... as so few do. – Morkrom May 05 '14 at 20:19
  • Is there a way to invoke that (by changing some kind of setting)? Or do we have to manually trigger ? – Khant Thu Linn Mar 02 '16 at 03:42
  • 1
    This appears to be deprecated –  Feb 17 '19 at 22:27