取消特定的UILocalNotification

我有本地通知的代码,我有一个scheduleNotification和clearNotification使用我自己的方法。 这些是代码:

- (void)clearNotification { [[UIApplication sharedApplication] cancelAllLocalNotifications]; } - (void)scheduleNotification { [reminderText resignFirstResponder]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; Class cls = NSClassFromString(@"UILocalNotification"); if (cls != nil) { UILocalNotification *notif = [[cls alloc] init]; notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30]; notif.timeZone = [NSTimeZone defaultTimeZone]; notif.alertBody = @"Evaluation Planner"; notif.alertAction = @"Details"; notif.soundName = UILocalNotificationDefaultSoundName; notif.applicationIconBadgeNumber = 1; NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text forKey:kRemindMeNotificationDataKey]; notif.userInfo = userDict; [[UIApplication sharedApplication] scheduleLocalNotification:notif]; [notif release]; } } 

这些代码工作正常,但现在我想知道如何知道它将删除哪个通知对象。 我想为通知创build一个ID,意思是一个ID等于一个通知。 但我不知道我应该在哪一部分做这件事。 另外,我需要find一种方法,包括所有这一切在一个plist。

希望有人能帮助我。 谢谢。

 NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification *not in notifications) { NSString *dateString=[not.userInfo valueForKey:@"EndDate"]; if([dateString isEqualToString:@"CompareString"]) { [[UIApplication sharedApplication] cancelLocalNotification:not]; } } 
  1. 无论何时创build本地通知(这是键值对),都要提供用户信息。
  2. 遍历通知(包含所有本地通知)并比较已知密钥的值。 在上面的例子中,我使用EndDate作为键,CompareString作为值。

它的工作与我很好。

干杯..

 (void)cancelLocalNotification:(NSString*)notificationID { // UILocalNotification *cancelThisNotification = nil; // BOOL hasNotification = NO; for (int j =0;j<[[[UIApplication sharedApplication]scheduledLocalNotifications]count]; j++) { UILocalNotification *someNotification = [[[UIApplication sharedApplication]scheduledLocalNotifications]objectAtIndex:j]; if([[someNotification.userInfo objectForKey:@"drdid"] isEqualToString:notificationID]) { NSLog(@"id,notificationID(App) %@ %@ ",[someNotification.userInfo objectForKey:@"drdid"],notificationID); NSLog(@"canceled notifications %@",someNotification); [[UIApplication sharedApplication] cancelLocalNotification:someNotification]; } } } 

我build议在UILocalNotification上使用userInfo属性,就像其他人所说的那样。 一个更简单的实现,接受的答案是:

 for(UILocalNotification* notification in [[UIApplication sharedApplication]scheduledLocalNotifications]) { if([[notification.userInfo objectForKey:@"notification_identifier"] isEqualToString:@"notification_001"]) { [[UIApplication sharedApplication] cancelLocalNotification:notification]; } } 

像这样的for循环要简单得多。 我不确定它是多less有点优化的,但它肯定比较容易阅读,我假设你只有几个通知来循环。