Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel UNNotificationRequest

Because UILocalNotification is now deprecated, I moved my code to the new UNNotificationRequest API.

It states: 'cancelLocalNotification' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removePendingNotificationRequestsWithIdentifiers:]

But it seems that it is not equal - while I could remove messages with cancelLocalNotification at any time (even they are displayed/delivered) it seems that removePendingNotificationRequestsWithIdentifiers only removes undelivered notifications. That's really annoying.

So my question is: Is there a valid way removing notifications queued with UNNotificationRequest or should I just ignore those deprecation warnings?

like image 732
coyer Avatar asked Aug 11 '17 12:08

coyer


People also ask

How do I clear all pending notifications in Swift?

removePendingNotificationRequests(withIdentifiers:) Removes your app's local notifications that are pending and match the specified identifiers.

What is UNMutableNotificationContent?

Overview. Create a UNMutableNotificationContent object when you want to specify the payload for a local notification. Specifically, use this object to specify the title and message for an alert, the sound to play, or the value to assign to your app's badge.


2 Answers

You can also use the removeDeliveredNotifications(withIdentifiers:) function to remove already delivered notifications from the notification center. For more info, see the documentation

like image 87
Dávid Pásztor Avatar answered Oct 06 '22 01:10

Dávid Pásztor


Swift 4 and Swift 5

If you want to remove all UNNotificationRequest items, you can use:

let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
like image 30
skymook Avatar answered Oct 06 '22 00:10

skymook