2010-07-01 38 views
47

我的UILocalNotification存在問題。取消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數據庫時,序列化對象,等等......是否有更智能的方法?

回答

57

您可以從scheduledLocalNotifications得到所有計劃通知的列表,也可以取消他們都:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 
+2

我瞭解cancelAllLocalNotifications但我只是想取消特定的。 ScheduledLocalNotifications可能做的工作....我會嘗試。 – f0rz 2010-07-01 13:47:36

+1

忘了告訴,它確實工作! :) – f0rz 2010-11-15 14:38:41

+1

你能分享如何? – Wasim 2011-10-11 09:40:25

13

我的解決方案是存檔你已經計劃以的NSKeyedArchiver UILocalNotification對象,並存儲在某個地方(在plist中是優選的)。然後,當您想要取消通知時,請向plist查找正確的數據並使用NSKeyedUnarchiver解除封存。 的代碼非常簡單:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice]; 

UILocalNotification *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
+0

您也可以使用Base64編碼將archievedDataWithRootObject的NSData序列化,然後保存到您已擁有的JSON對象中。 – 2012-04-28 17:03:46

+0

@RafaelSanches對不起,線程necro。 但是如果目標是存儲/檢索uilocalnotification以便以後刪除,使用JSON對象有什麼優勢? – iamdavidlam 2015-05-04 09:10:19

6

我的解決方法是,當你在那個時候建立UILocalNotification創建一個NSMutableDictionary並存儲該通知的值鍵作爲你的ID和把這NSMutableDictionay到你的NSUserDefaults

所以,當你想取消任何特定localnotification當時你寫 [dictionary valueforkey @"KEY"],其中的一個關鍵你通過你的ID,以便您獲得特定的本地通知,並將其傳遞給

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
91

我的解決辦法是使用UILocalNotificationUSERINFO字典。實際上,我所做的是爲我的每個通知生成一個唯一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]; 

我不知道這種做法是好是壞相對於存檔/ Unarchving之一,但它的工作原理和限制數據被保存到只是一個ID。

編輯:有一個丟失的託槽

+0

我試過你的方法,並沒有爲我工作。你能告訴我什麼是錯的嗎? http://stackoverflow.com/questions/9139586/cancel-local-notification-not-working – Farini 2012-02-04 08:45:35

+2

你沒有在你的解決方案中使用我的方法。我的解決方案繞過一個ID,而您使用整個對象。從你的代碼看來,通知對象有一些早期的解除分配,這就是爲什麼「無法識別的選擇器」(這通常發生在舊對象位置被另一種對象重用時)。嘗試在發送cancelNotification消息來挖掘它之前獲取對象的類名,這可能有助於調試。此外,當您分配「theNotification」使用「self.theNotification = ...「而不是」theNotificaiton = ...「 – viggio24 2012-02-04 09:47:58

+0

非常感謝你!你實際上啓發了我在每個單元格上添加一個按鈕,我可以:cancelLocalNotification atindex [發件人標籤],它帶來了uialertview更好的效果。說對象的名稱現在就在取消之前,它工作得很好! – Farini 2012-02-04 20:03:16

0

我的解決方法是刪除這些方法的所有計劃通知。

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

[[UIApplication的sharedApplication] cancelAllLocalNotifications];

您仍然可以通過使用來獲取激活您的應用程序的LocalNotification對象。

UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

然後我就

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

這就避免了有記賬的所有通知,重新安排新的通知。我還在userInfo字典中發送了一些上下文信息。這有助於跳轉到應用程序的正確位置。對好答案

0

謝謝@ 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++ 
} 
+0

沒有必要while,如果讓通知,只要保持它簡短''通知notifyArray爲[UILocalNotification] {如果讓info = notifier.userInfo爲? Dictionary {...}}' – 2015-04-01 13:54:25

1

斯威夫特版本取消特定的「本地通知」使用用戶信息:

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!") 
      } 
     } 
    } 
3

在急速的,你先加一個字典通知userInfo通過:

let dict:NSDictionary = ["ID" : "someString"] 
notification.userInfo = dict as! [String : String] 

然後,你想獲得一個現有的notifi數組陽離子(2),並遍歷每一個(3),直到找到你要找的那個。一旦找到它,取消它(4)。

// 1. Pass in the string you want to cancel 
func cancelLocalNotification(uniqueId: String){ 

    // 2. Create an array of notifications, ensuring to use `if let` so it fails gracefully 
    if let notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications { 

     // 3. For each notification in the array ... 
     for notif in notifyArray as [UILocalNotification] { 
      // ... try to cast the notification to the dictionary object 
      if let info = notif.userInfo as? [String: String] { 

       // 4. If the dictionary object ID is equal to the string you passed in ... 
       if info["ID"] == uniqueId { 
        // ... cancel the current notification 
        UIApplication.sharedApplication().cancelLocalNotification(notif) 
       } 
      } 
     } 
    } 
} 
+0

注意:UIApplication.sharedApplication()現在是UIApplication.shared – 2017-10-22 06:04:49

1

斯威夫特版本:

UIApplication.sharedApplication().cancelAllLocalNotifications()