Note that there are some explanatory texts on larger screens.

plurals
  1. POIf two local notifications have the same fire time and the app is in the background then didReceiveLocalNotification is only called once
    text
    copied!<p>This is easy to reproduce - and looks like a bug with iOS? If its a feature, then how to tell if the 2nd notification has been accepted or not?</p> <hr> <p>UPDATE: This isn't limited in scope to two or more notifications with the same fire date, they can have any fire date but if they both expire while the app is in the background then its still the same result - didReceiveLocalNotification is only called once.</p> <hr> <p>If two local notifications are scheduled with the same fire date and the app is in the background (applicationDidEnterBackground: was the last method to be called) then this is the sequence of steps that occurs when the notifications fire:</p> <p>1) The two notifications are displayed to the user</p> <p>2) The user clicks on first notification</p> <p>3) applicationWillEnterForeground: is called as a consequence of step 2</p> <p>4) didReceiveLocalNotification: is called</p> <p>5) applicationDidBecomeActive: is NOT called, nothing happens further until step 6</p> <p>6) The user clicks on the second notification</p> <p>7) Now applicationDidBecomeActive is called as a consequence of step 6 </p> <p>8) didReceiveLocalNotification is NOT called a 2nd time after step 6</p> <p>The consequence of this is the app does NOT receive any indication that more than one local notification was accepted by the user.</p> <p>This is easy to reproduce - create a new project, add the following code, run then exit the app before the notifications have triggered.</p> <p>-</p> <pre><code> (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"didFinishLaunchingWithOptions"); self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; NSDate* date = [[NSDate alloc] initWithTimeIntervalSinceNow:20]; UILocalNotification *notif1 = [[UILocalNotification alloc] init]; notif1.fireDate = date; notif1.timeZone = [NSTimeZone defaultTimeZone]; notif1.alertBody = @"Notif1"; UILocalNotification *notif2 = [[UILocalNotification alloc] init]; notif2.fireDate = date; notif2.timeZone = [NSTimeZone defaultTimeZone]; notif2.alertBody = @"Notif2"; [[UIApplication sharedApplication] scheduleLocalNotification:notif1]; [[UIApplication sharedApplication] scheduleLocalNotification:notif2]; NSLog(@"Notifications scheduled"); return YES; } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"didReceiveLocalNotification: %@", notification.alertBody); } </code></pre> <p>This is the console output:</p> <p>2012-02-01 21:32:12.832 NotificationsTest[13223:f803] didFinishLaunchingWithOptions 2012-02-01 21:32:15.755 NotificationsTest[13223:f803] Notifications scheduled 2012-02-01 21:32:15.756 NotificationsTest[13223:f803] applicationDidBecomeActive: 2012-02-01 21:32:22.838 NotificationsTest[13223:f803] applicationWillResignActive: 2012-02-01 21:32:22.840 NotificationsTest[13223:f803] applicationDidEnterBackground:</p> <p>[the two notifications are displayed, with Notif2 on top of Notif1]</p> <p>[User views Notif2]</p> <p>2012-02-01 21:33:34.177 NotificationsTest[13223:f803] applicationWillEnterForeground: 2012-02-01 21:33:34.179 NotificationsTest[13223:f803] didReceiveLocalNotification: Notif2</p> <p>[User views Notif1]</p> <p>2012-02-01 21:33:58.278 NotificationsTest[13223:f803] applicationDidBecomeActive:</p> <p>How can the app get information that the 2nd notification has expired and its contents?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload