Iphone 取消本地通知

Iphone 取消本地通知,iphone,cocoa-touch,notifications,ios4,Iphone,Cocoa Touch,Notifications,Ios4,我的UILocalNotification有问题 我正在使用我的方法安排通知 - (void) sendNewNoteLocalReminder:(NSDate *)date alrt:(NSString *)title { // some code ... UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) re

我的UILocalNotification有问题

我正在使用我的方法安排通知

- (void) sendNewNoteLocalReminder:(NSDate *)date  alrt:(NSString *)title
{
    // some code ...
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

    if (localNotif == nil)  
        return;

    localNotif.fireDate = itemDate; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil); 
    localNotif.alertBody = title;
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 0;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"id"]; 
    localNotif.userInfo = infoDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release];
}
工作正常,我正确地收到了通知。问题是我什么时候应该取消通知。我在用这个方法

- (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE
{
    [[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ????  
}
我不知道该怎么办,但我的问题是:

如何知道应该删除哪个UILocalNotification对象?
是否有办法列出所有通知

我唯一拥有的是我应该删除的提醒的ID。

我正在考虑将UILocalNotification对象保存在我的“Note”对象中,并以这种方式获取它,当我保存到我的SQLite数据库时,序列化该对象等等。。。有更聪明的方法吗

您可以从中获取所有计划通知的列表,也可以全部取消这些通知:

  [[UIApplication sharedApplication] cancelAllLocalNotifications];

我的解决方案是归档您使用NSKeyedArchiver计划的UILocalNotification对象,并将其存储在某个位置(首选plist)。然后,当您想要取消通知时,可以在plist中查找正确的数据,并使用NSKeyedUnarchiver取消归档。 代码非常简单:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice];


我的解决方案是,当您创建
UILocalNotification
时,您可以创建一个
NSMutableDictionary
并将该通知作为密钥的值存储为您的ID,然后将该
NSMutableDictionay
放入您的
NSUserDefaults

因此,当您想取消任何特定的本地通知时,您可以编写
[dictionary valueforkey@“KEY”]
其中作为密钥传递您的id,以便获得特定的本地通知并将其传递给

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

我的解决方案是使用
UILocalNotification
userInfo字典。事实上,我要做的是为我的每个通知生成一个唯一ID(当然这个ID是我以后可以检索到的),然后当我想取消与给定ID关联的通知时,我只需使用数组扫描所有可用的通知:

[[UIApplication sharedApplication] scheduledLocalNotifications]
然后我通过调查ID来尝试匹配通知。例如:


NSString *myIDToCancel = @"some_id_to_cancel";
UILocalNotification *notificationToCancel=nil;
for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
  if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) {
     notificationToCancel=aNotif;
     break;
  }
}
if(notificationToCancel) [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
我不知道这种方法相对于归档/非归档方法是否更好,但是它可以工作,并且将数据保存到一个ID


编辑:缺少braket

我的解决方案是删除这些方法上的所有计划通知

-applicationDidFinishLaunchingWithOptions: 
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

[[UIApplication sharedApplication]取消所有本地通知]

您仍然可以使用访问激活应用程序的LocalNotification对象

    UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
然后我重新安排在

- (void)applicationDidEnterBackground:(UIApplication *)application;

这避免了对所有通知进行簿记。我在userInfo字典中也发送了一些上下文信息。这有助于跳转到应用程序中的正确位置。

感谢@viggio24给出的好答案,这是一个快速版本

var notifyCancel = UILocalNotification()
var notifyArray=UIApplication.sharedApplication().scheduledLocalNotifications
var i = 0
while(i<notifyArray.count){
    if let notify = notifyArray[i] as? UILocalNotification{
        if let info = notify.userInfo as? Dictionary<String,String> {
            if let s = info["name"] {
                if(name==s){//name is the the one you want cancel
                    notifyCancel = notify
                    break
                }
            }
        }
    }
i++
}
var notifyCancel=UILocalNotification()
var notifyArray=UIApplication.sharedApplication().ScheduledLocalNotification
变量i=0

而(iSwift版本)则使用userinfo取消特定的“本地通知”:

func cancelLocalNotification(UNIQUE_ID: String){

        var notifyCancel = UILocalNotification()
        var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications

        for notifyCancel in notifyArray as! [UILocalNotification]{

            let info: [String: String] = notifyCancel.userInfo as! [String: String]

            if info[uniqueId] == uniqueId{

                UIApplication.sharedApplication().cancelLocalNotification(notifyCancel)
            }else{

                println("No Local Notification Found!")
            }
        }
    }

在swift中,首先通过以下方式将dict添加到通知userInfo:

let dict:NSDictionary = ["ID" : "someString"]
notification.userInfo = dict as! [String : String]
然后,您希望获得一个现有通知数组(2),并对每个通知进行迭代(3),直到找到您要查找的通知。一旦找到,请取消它(4)


Swift版本:

UIApplication.sharedApplication().cancelAllLocalNotifications()
Swift 5:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)

我知道cancelAllLocalNotifications,但我只想取消一个特定的。ScheduledLocalNotifications可能会起作用…我会尝试。我尝试了你的方法,但对我不起作用。你能告诉我怎么了吗?你的解决方案中没有使用我的方法。我的解决方案传递一个ID,而你使用整个对象。它似乎是从y开始的我们的代码表明,通知对象存在一些早期的dealloc,这就是为什么会出现“无法识别的选择器”(这通常发生在旧对象位置被另一种对象重用时)。在发送cancelNotification消息之前尝试获取对象的类名,这可能有助于调试。此外,在指定“通知”时,请使用“self.theNotification=…”而不是“Notification=…”非常感谢!你实际上启发了我在每个单元格中添加一个按钮:CanceloCalNotification atindex[发送者标签],它的效果比使用uialertview要好得多。正如你所说,对象的名称现在正好在取消之前,它的效果很好!是的,我看到了你的“自我回答”在您的问题中。我同意您的看法,即添加按钮然后取消其选择器上的通知对于您的特定情况完全有效。如果使用
CoreDate
我发现使用唯一的托管对象ID很有帮助:
NSManagedObjectID*moID=[managedObject objectID];
。由于
CoreData
仅在执行保存操作后分配一个永久ID,因此您可以使用:
BOOL istemporation=[[managedObject objectID]istemporationid]
您还可以使用Base64编码将ArchiveDDataWithRootObject中的NSData序列化,然后保存到您已有的JSON对象中。@RafaelSanches对线程necro感到抱歉。但是,如果目标是存储/检索uilocalnotification以备以后删除,那么使用JSON对象有什么好处?不需要花费时间,如果让notify,只需在notifyArray中使用
作为[UILocalNotification]{if let info=notifier.userInfo as?Dictionary{…}}
注意:UIApplication.sharedApplication()现在是UIApplication.shared,以防任何Xamarin开发人员都会寻找答案。这一个对我来说非常有效。
UNUserNotificationCenter.Current.RemovePendingNotificationRequests(新字符串[]{NotificationId});
UIApplication.sharedApplication().cancelAllLocalNotifications()
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)