19

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)
        }
    })
}
Denis Kakačka
  • 697
  • 1
  • 8
  • 21

3 Answers3

45

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)
}
KrishnaCA
  • 5,615
  • 1
  • 21
  • 31
  • Inside AppDelegate where we want to call this function?, inside `didReceiveRemoteNotification` or `application(received remoteMessage: MessagingRemoteMessage)` – Kavin Kumar Arumugam Jun 20 '18 at 08:25
  • 2
    This is for cancelling a scheduled local notification. It can be called from anywhere since `UNUserNotificationCenter.current()` is a singleton object – KrishnaCA Jun 21 '18 at 19:48
12

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.

Zac Kwan
  • 5,587
  • 4
  • 20
  • 27
-5

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

UIApplication.sharedApplication().cancelAllLocalNotifications()

Community
  • 1
  • 1
Yannick Winter
  • 103
  • 2
  • 11