2013-07-09 108 views
3

我有一個場景,我安排了兩個單獨的LocalNotifications。一個人在午夜十點鐘開火,另一個每小時開火。我可以通過在安排通知時將UserInfo設置爲通知來區分它們。cancelAllLocalNotifications With Multiple LocalNotifications

對於每小時通知,我會在每次通知觸發時在覈心數據中創建一個項目。但問題是,每小時通知一次,我在Core Data每次啓動時都會收到很多額外的項目。

我不會調用以下代碼行,因爲它似乎也取消了我應該在午夜後觸發的通知。

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

當我有多個預定LocalNotifications時,我應該如何使用cancelAllLocalNotifications?

回答

3

是的,如你所見。如其名稱所示,cancelAllLocalNotifications將取消一切。是的,你也應該使用userInfo來區分你的通知。

要管理/刪除您的通知,選擇你應該得到所有註冊的通知與scheduledLocalNotifications,然後在它們之間迭代檢查userInfo,然後調用cancelLocalNotification與您不再需要的人。

+0

正是我所需要的解釋。谷歌一直讓我失望! – mix3d

1

由於斯威夫特3.X發生了,這是必要的,以消除所有本地推送通知的代碼:

let notificationCenter : UNUserNotificationCenter = UNUserNotificationCenter.current() 
notificationCenter.removeAllPendingNotificationRequests() 
notificationCenter.removeAllDeliveredNotifications()