IOS, how to handle multiple local notifications wh

2019-08-06 11:05发布

问题:

I'm creating an app that uses timers. Let's say that the user can set multiple timers; for each of those timers the app schedule a local notification. When the app is running in foreground or is in background i have no problem handling multiple local notifications. my problem is when the user set multiple timers and then terminate the app( double click on home button and close the app). in that case, when timers expire all relative local notifications are shown as a banner and the app icon badge is incremented. so i want to handle all of those notifications when the user start the app from notification banner or tapping on app icon but using

didFinishLaunchingWithOptions

I am able to handle only one notification with

[launchOptions UIApplicationLaunchOptionsLocalNotificationKey]

I need to handle all local notifications of all timers!! how can i do that?

回答1:

You can add an id to each local notification so you know from which notification was the app triggered:

localNotification1 = [[UILocalNotification alloc] init]; 
localNotification1.userInfo = @{ "type" : @1 };
...
localNotification2 = [[UILocalNotification alloc] init]; 
localNotification2.userInfo = @{ "type" : @2 };

http://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html#//apple_ref/occ/instp/UILocalNotification/userInfo