How to Read and Cancel Local Notification Requests

In this tutorial, you will learn how to read and cancel local user notification request(UNNotificationRequest) or how to remove the pending or even delivered notifications from a User Notification Center UNUserNotificationCenter.

To learn how to create, schedule and display user local notifications read the following tutorial:  User Local Notifications with UNUserNotificationCenter.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Notification Request Identifier

When a UNNotificationRequest is created, it is created with an identifier.

let notificationRequest = UNNotificationRequest(identifier: UUID().uuidString, content: notification, trigger: trigger)

The UNNotificationRequest identifier is a String value and should be unique for each notification request. In the code snippet above, the UUID is used as a UNNotificationRequest identifier.

To be able to access a Notification Request in the UNUserNotificationCenter we need to know the notification identifier. Once we know the identifier, we can then remove this request from a notification center.

Access All NotificationRequests

We can access the UNUserNotificationCenter and read all pending notification requests. Having access to a UNNotificationRequest we can read any property this object has. In the code snippet below I am printing out the notification request identifier. I could also print any other property of the notification request object.

notificationCenter.getPendingNotificationRequests { (notificationRequests) in
     for notificationRequest:UNNotificationRequest in notificationRequests {

Remove UNNotificationRequest From UNUserNotificationCenter

To remove UNNotificationRequest from a UNUserNotificationCenter we will need to use the removePendingNotificationRequests() function that accepts an array of notification request identifiers. To remove a single notification, simply provide a single identifier in the list.

notificationCenter.removePendingNotificationRequests(withIdentifiers: [notificationIdentifier])

Remove All Notification Requests

We can also remove all pending or even delivered notifications requests from the user notification center and to do that we do not need to know notification request identifiers.

Remove Pending Notification Requests

To remove all pending notification requests use the removeAllPendingNotificationRequests() method.


Remove Delivered Notification Requests

To remove all delivered notification requests use the removeAllDeliveredNotifications() method.


Read All Delivered Notifications

We can access the notification center and read all delivered notifications. In the code snippet below I am printing a unique identifier of a delivered notification. I could also print any other property UNNotification object has.

notificationCenter.getDeliveredNotifications { (notifications) in
    for notification:UNNotification in notifications {

I hope this tutorial was of some value to you. Have a look at other Swift tutorials on this web site or use the Search box to search for what you need by a keyword.  There are many interesting Swift tutorials you might find.

Happy Swift coding 🙋🏻‍♂️!

Leave a Reply

Your email address will not be published. Required fields are marked *