在安排通知时,您可以设置通知用户信息的一些唯一ID。
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notif.timeZone = [NSTimeZone defaultTimeZone];
// set the your data with unique id
NSMutableDictionary *dict=[NSMutableDictionary new];
[dict setObject:Id forKey:@"id"];
// assignt the dictionary to user info
notif.userInfo=dict;
notif.alertBody = @"test Notification";
notif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
你可以从didReceiveLocalNotification获得用户信息这样的
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([[notification.userInfo valueForKey:@"id"] isEqualToString:@"1"])
{
NSLog(@"notification id %@",[notification.userInfo valueForKey:@"id"]);
}
else if ([[notification.userInfo valueForKey:@"id"] isEqualToString:@"2"])
{
NSLog(@"notification id %@",[notification.userInfo valueForKey:@"id"]);
}
////// or /////
if ([notification.userInfo valueForKey:@"id"])
{
NSLog(@"id of notification %@",[notification.userInfo valueForKey:@"id"]);
}
}
从didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey])
{
UILocalNotification *notif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(@"notif.userInfo %@",notif.userInfo);
// notif.userInfo {
// id = 2;
// }
}
return YES;
}
根据我的经验(我只是写了一个小的测试应用程序,日程安排3个UILocalNotifications与当应用程序被关闭,以测试这同样fireDate),当用户点击他们的通知画面指定警报,从而启动应用程序,传递给UIApplication的didReceiveNotifications方法的唯一UILocalNotification是用户点击的方法。所以我不太了解你的问题。 – vaticRite
它不回答你的问题,但是只有一个本地通知(即取消以前的通知)。如果这样做,则可以跟踪通知的userInfo字典,以便在应用程序变为活动状态时以后使用。如果你仍然需要多个本地通知,是否可以将它分组成一个单一的选项?如果是,您可以准备一些用户信息字典数组,以便在应用程序变为活动状态时使用。 – atxe
不能使用用户信息字典找出哪些通知已打开,并检查didRecieveLocalNotif, –