取消特定的UILocalNotification [英] Cancelling a specific UILocalNotification

查看:76
本文介绍了取消特定的UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this code for local notification, and I have a scheduleNotification and clearNotification using my own method. These are the codes:

- (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中。

These codes works well, but now I want to know how do I know which notification object will it delete. I would like to create an ID for a notification, meaning, one ID is equivalent to one notification. But I don't know at which part I should do that. Plus I need to find a way to include all this to be in a plist.

希望有人可以帮助我。谢谢。

Hope somebody can help me. Thanks.

推荐答案

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作为值。

它与我合作。

干杯..

这篇关于取消特定的UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆