5

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

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)

有没有办法让我在设置触发器后取消它?

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

4

2 回答 2

13

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

let center = UNUserNotificationCenter.current()

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

center.removePendingNotificationRequests(withIdentifiers: [“givenIdentifier”])

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

center.removeDeliveredNotifications(withIdentifiers: [“givenIdentifier”])
于 2019-03-24T14:46:10.533 回答
0

建立在曼诺普森的回答之上:

这将删除所有待处理的通知(Swift 5

func cancelNotifications() -> Void {
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
于 2020-05-05T11:06:18.957 回答