motionpotion
motionpotion

Reputation: 2736

cancelAllLocalNotifications With Multiple LocalNotifications

I have a scenario where I schedule two separate LocalNotifications. One is set to fire at ten past midnight and the other fires every hour. I can distinguish between them by setting the UserInfo for the notification when I am scheduling it.

For the hourly notification I create an item in Core Data each time that notification fires. But the problem is that for the hourly notification I am getting a lot of extra items in Core Data each time it fires.

I am NOT calling the following line of code because calling it it seems to also cancel my notification that is supposed to fire after midnight.

[[UIApplication sharedApplication] cancelAllLocalNotifications];

How should I use cancelAllLocalNotifications when I have multiple scheduled LocalNotifications?

Upvotes: 3

Views: 2137

Answers (2)

LukeSideWalker
LukeSideWalker

Reputation: 7930

Since Swift 3.x took place, this is the code necessary to remove all local push notifications:

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

Upvotes: 1

Wain
Wain

Reputation: 119041

Yes, as you see. cancelAllLocalNotifications, as it's name suggests, will cancel everything. Yes, also, you should use the userInfo to differentiate between your notifications.

To manage / remove your notifications selectively you should get all of the registered notifications with scheduledLocalNotifications, then iterate over them checking the userInfo, then call cancelLocalNotification with the ones you no longer need.

Upvotes: 3

Related Questions