取消特定的 UILocalNotification

2024-03-04

我有这个用于本地通知的代码,并且我有一个使用我自己的方法的 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];
    }
}

这些代码运行良好,但现在我想知道如何知道它将删除哪个通知对象。我想为一个通知创建一个ID,也就是说,一个ID相当于一个通知。但我不知道我应该在哪一部分这样做。另外,我需要找到一种方法将所有这些都包含在一个 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. 每当您创建本地通知时都提供用户信息(这是一个键值对)。
  2. 迭代通知(它包含所有本地通知)并比较已知键的值。在上面的示例中,我使用 EndDate 作为键,使用 CompareString 作为值。

它对我来说工作得很好。

Cheers..

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

取消特定的 UILocalNotification 的相关文章

随机推荐