How to cancel UserNotifications
Asked Answered
H

3

21

How I can disable/cancel already setted notification?

Here is my schedule function.

func scheduleNotif(date: DateComponents, completion: @escaping (_ Success: Bool) -> ()) {

    let notif = UNMutableNotificationContent()

    notif.title = "Your quote for today is ready."
    notif.body = "Click here to open an app."

    let dateTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
    let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in

        if error != nil {
            print(error)
            completion(false)
        } else {
            completion(true)
        }
    })
}
Hoff answered 12/11, 2016 at 12:38 Comment(0)
L
47

For cancelling all pending notifications, you can use this:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

For cancelling specific notifications,

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
   var identifiers: [String] = []
   for notification:UNNotificationRequest in notificationRequests {
       if notification.identifier == "identifierCancel" {
          identifiers.append(notification.identifier)
       }
   }
   UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
Latvina answered 12/11, 2016 at 14:0 Comment(2)
Inside AppDelegate where we want to call this function?, inside didReceiveRemoteNotification or application(received remoteMessage: MessagingRemoteMessage)Gloxinia
This is for cancelling a scheduled local notification. It can be called from anywhere since UNUserNotificationCenter.current() is a singleton objectLatvina
O
13

The way same UNNotification is identify are base on the identifier passed when you create the UNNotificationRequest.

In your example above,

let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)

You have actually hardcoded the identifier to be "myNotif". This way, whenever you want to delete the already set notification you can do this:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "myNotif")

However, do take note that when you hardcode the identifier, every time when you add a request to UNUserNotificationCenter the notification is actually being replaced.

Example, if you scheduled a "myNotif" request set on 1 minute later but you call another function to schedule a "myNotif" at 1 hour later it will be replaced. Therefore only the latest "myNotif" at one hour later will be in the pendingNotificationRequest.

Oyez answered 12/11, 2016 at 14:5 Comment(0)
T
-5

As mentioned here you can cancel all notifications with the following code:

UIApplication.sharedApplication().cancelAllLocalNotifications()

Tambourin answered 12/11, 2016 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.