I have a trigger to show a notification to the user:
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)
let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
Is there a way for me to cancel that trigger once it has been set?
How do I cancel the notification before the timeInterval
runs out and call the notification?
The swift file (say ViewController. swift ) in which you want to create local notification should contain below code: //MARK: - Button functions func buttonIsPressed(sender: UIButton) { println("buttonIsPressed function called \(UIButton. description())") var localNotification = UILocalNotification() localNotification.
If you run your app now, press the first button to request notification permission, then press the second to add an actual notification. Now for the important part: once your notification has been added press Cmd+L in the simulator to lock the screen.
You can cancel or remove notifications by calling:
let center = UNUserNotificationCenter.current()
Remove pending notifications with given identifier
center.removePendingNotificationRequests(withIdentifiers: [“givenIdentifier”])
And remove delivered notifications with given identifier
center.removeDeliveredNotifications(withIdentifiers: [“givenIdentifier”])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With