如何取消 Swift 中的本地通知触发器

Chi*_*dog 4 swift unnotificationrequest unusernotificationcenter

我有一个触发器来向用户显示通知:

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)
Run Code Online (Sandbox Code Playgroud)

一旦设置了该触发器,我有没有办法取消它?

如何在timeInterval用完之前取消通知并调用通知?

Man*_*son 10

您可以通过以下方式取消或删除通知:

let center = UNUserNotificationCenter.current()
Run Code Online (Sandbox Code Playgroud)

删除具有给定标识符的待处理通知

center.removePendingNotificationRequests(withIdentifiers: [“givenIdentifier”])
Run Code Online (Sandbox Code Playgroud)

并删除具有给定标识符的传递通知

center.removeDeliveredNotifications(withIdentifiers: [“givenIdentifier”])
Run Code Online (Sandbox Code Playgroud)