I'm sending a notification to the user with the following code using UNTimeIntervalNotificationTrigger
. The notification is sent after 1 hour, that works. Now I want to give the user the ability to reset the TimeInterval for the notification so that the same notification runs again, but the TimeInterval only resets when the user presses this button. This means that repeats: true
is not an option.
My code:
let tijd = 15
@IBAction func change(_ sender: Any) {
// Notification
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.badge = 1
content.sound = UNNotificationSound.default()
// Timer
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
let request = UNNotificationRequest(identifier: bezigheid, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
@IBAction func repeat(_ sender: Any) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
trigger.invalidate()
}
I tried invalidating the TimeInterval when clicking the button repeat, but this only gave me an error so I don't think this is the way to go. What is the way do preform this action? :)
First remove the old notification using the following code: // Remove notification
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [bezigheid])
Then you can set up the next notification just like you set up the first notification!