UserNotifications cancel, swift3

I am working on an application where the user can set the time for daily local notification, then I have the ability to enable or disable these notifications. My question is, how can I disable / cancel an already installed notification?

Here in one of these IFs I need to cancel notif

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if (indexPath as NSIndexPath).row == 1 && switchiBtn.isOn == false { return 0.0 } if (indexPath as NSIndexPath).row == 2 { if switchiBtn.isOn == false || dpVisible == true { return 0.0 } return 216.0 } if (indexPath as NSIndexPath).row == 3 { if switchiBtn.isOn == false || dpVisible == true { return 0.0 } return 44 } return 50.0 } 

Here are the functions for the schedule and buttons where I install notif.

 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) } }) } @IBAction func setACTION(_ sender: AnyObject) { let hour = datePicker.calendar.component(.hour, from: datePicker.date) let minute = datePicker.calendar.component(.minute, from: datePicker.date) var dateComponents = DateComponents() dateComponents.hour = hour dateComponents.minute = minute print(dateComponents) scheduleNotif(date: dateComponents, completion: { success in if success { print("SUCCESS") } else { print("ERROR") } }) 

Thanks.

+5
source share
3 answers

To cancel all pending notifications, you can use this:

 UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 

To cancel certain 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) } 
+17
source

The same UNNotification is the base for the identifier passed when creating the UNNotificationRequest .

In the example above

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

You really have hardcoded identifier as "myNotif" . Thus, whenever you want to delete an already installed notification, you can do this:

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

However, note that with hard-coded identifiers every time you add request to UNUserNotificationCenter , the notification is actually replaced.

For example, if you scheduled the "myNotif" request install 1 minute later, but you call another function to schedule the "myNotif" 1 hour later, it will be replaced. Therefore, only the last "myNotif" in an hour will be in pendingNotificationRequest .

+5
source

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

UIApplication.sharedApplication().cancelAllLocalNotifications()

-3
source

Source: https://habr.com/ru/post/1259629/


All Articles