如何取消用户通知
How to cancel UserNotifications
如何disable/cancel设置通知?
这是我的日程安排功能。
func scheduleNotif(date: DateComponents, completion: @escaping (_ Success: Bool) -> ()) {
let notif = UNMutableNotificationContent()
notif.title = "Your quote for today is ready."
notif.body = "Click here to open an app."
let dateTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print(error)
completion(false)
} else {
completion(true)
}
})
}
如前所述您可以使用以下代码取消所有通知:
UIApplication.sharedApplication().cancelAllLocalNotifications()
要取消所有待处理的通知,您可以使用:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
取消特定通知,
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
var identifiers: [String] = []
for notification:UNNotificationRequest in notificationRequests {
if notification.identifier == "identifierCancel" {
identifiers.append(notification.identifier)
}
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
相同 UNNotification
的识别方式是基于创建 UNNotificationRequest
.
时传递的 identifier
在你上面的例子中,
let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
您实际上已经将 identifier
硬编码为 "myNotif"
。这样,每当你想删除已经设置的通知时,你可以这样做:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "myNotif")
但是,请注意,当您对标识符进行硬编码时,每次将 request
添加到 UNUserNotificationCenter
时,通知实际上都会被替换。
例如,如果您将 "myNotif"
request
安排在 1 分钟后,但您调用另一个函数将 "myNotif"
安排在 1 小时后,它将被替换。因此只有一小时后的最新 "myNotif"
才会出现在 pendingNotificationRequest
.
中
如何disable/cancel设置通知?
这是我的日程安排功能。
func scheduleNotif(date: DateComponents, completion: @escaping (_ Success: Bool) -> ()) {
let notif = UNMutableNotificationContent()
notif.title = "Your quote for today is ready."
notif.body = "Click here to open an app."
let dateTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print(error)
completion(false)
} else {
completion(true)
}
})
}
如前所述
UIApplication.sharedApplication().cancelAllLocalNotifications()
要取消所有待处理的通知,您可以使用:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
取消特定通知,
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
var identifiers: [String] = []
for notification:UNNotificationRequest in notificationRequests {
if notification.identifier == "identifierCancel" {
identifiers.append(notification.identifier)
}
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
相同 UNNotification
的识别方式是基于创建 UNNotificationRequest
.
identifier
在你上面的例子中,
let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
您实际上已经将 identifier
硬编码为 "myNotif"
。这样,每当你想删除已经设置的通知时,你可以这样做:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "myNotif")
但是,请注意,当您对标识符进行硬编码时,每次将 request
添加到 UNUserNotificationCenter
时,通知实际上都会被替换。
例如,如果您将 "myNotif"
request
安排在 1 分钟后,但您调用另一个函数将 "myNotif"
安排在 1 小时后,它将被替换。因此只有一小时后的最新 "myNotif"
才会出现在 pendingNotificationRequest
.