我正在使用UNTimeIntervalNotificationTrigger
使用以下代码向用户发送通知。通知将在1小时后发送,并且可以正常工作。现在,我想让用户能够重置通知的TimeInterval,以便再次运行相同的通知,但是TimeInterval仅在用户按下此按钮时重置。这意味着repeats: true
不是一个选项。
我的代码:
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()
}
我在单击按钮重复按钮时尝试使TimeInterval无效,但这只给了我一个错误,所以我不认为这是要走的路。执行此操作的方式是什么? :)
最佳答案
首先使用以下代码删除旧的通知://删除通知
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [bezigheid])
然后,您可以像设置第一个通知一样设置下一个通知!