iphone - 取消特定的 UILocalNotification

标签 iphone objective-c ios notifications

我有这个本地通知代码,我有一个 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 作为值。

它对我很好。

干杯..

关于iphone - 取消特定的 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7186236/

相关文章:

iphone - 闪烁时间戳

iphone - 我的 NSLog 输出在哪里?

ios - 用于 webView 的 scalesPageToFit 不适用于 iOS 9.3

ios - 在 Swift 中继续之前等待 Objective-C 函数的结果

ios - 分享扩展状态栏样式

iphone - 通过点击放大 uiwebview?

iphone - 核心动画警告 : "uncommitted CATransaction"

iOS - 旋转时使用动态单元格高度重绘 TableViewCells

android - 适用于手机的 Google Maps API 夜间主题

ios - 谷歌地图自动完成改变颜色