xcode 用户通知取消,swift3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40562912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UserNotifications cancel, swift3
提问by deniskakacka
I'm working on app where user can set time for everyday local notification, then I have an option to enable or disable these notifications. My question how I can disable/cancel already setted notification?
我正在开发应用程序,用户可以在其中设置每天本地通知的时间,然后我可以选择启用或禁用这些通知。我的问题是如何禁用/取消已设置的通知?
Here in one of this IFs I need to cancel notif
在此 IF 之一中,我需要取消通知
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath as NSIndexPath).row == 1 && switchiBtn.isOn == false {
return 0.0
}
if (indexPath as NSIndexPath).row == 2 {
if switchiBtn.isOn == false || dpVisible == true {
return 0.0
}
return 216.0
}
if (indexPath as NSIndexPath).row == 3 {
if switchiBtn.isOn == false || dpVisible == true {
return 0.0
}
return 44
}
return 50.0
}
Here are functions for schedule and button where I'm setting notif.
这是我设置通知的日程表和按钮的功能。
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)
}
})
}
@IBAction func setACTION(_ sender: AnyObject) {
let hour = datePicker.calendar.component(.hour, from: datePicker.date)
let minute = datePicker.calendar.component(.minute, from: datePicker.date)
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
print(dateComponents)
scheduleNotif(date: dateComponents, completion: { success in
if success {
print("SUCCESS")
} else {
print("ERROR")
}
})
Thanks.
谢谢。
回答by KrishnaCA
For cancelling all pending notifications, you can use this:
要取消所有挂起的通知,您可以使用:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
For cancelling specific notifications,
如需取消特定通知,
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)
}
回答by Zac Kwan
The way sameUNNotification
is identify are base on the identifier
passed when you create the UNNotificationRequest
.
相同的方式UNNotification
是基于identifier
创建UNNotificationRequest
.
In your example above,
在你上面的例子中,
let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
You have actually hardcoded the identifier
to be "myNotif"
. This way, whenever you want to delete the already set notification you can do this:
你确实有硬编码的identifier
是"myNotif"
。这样,无论何时您想删除已设置的通知,您都可以执行以下操作:
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "myNotif")
However, do take note that when you hardcode the identifier, every time when you add a request
to UNUserNotificationCenter
the notification is actually being replaced.
但是,请注意,当您对标识符进行硬编码时,每次request
向UNUserNotificationCenter
通知添加 a 时实际上都被替换了。
Example, if you scheduled a "myNotif"
request
set on 1 minute later but you call another function to schedule a "myNotif"
at 1 hour later it will be replaced. Therefore only the latest "myNotif"
at one hour later will be in the pendingNotificationRequest
.
例如,如果您"myNotif"
request
在 1 分钟后安排了一个集合,但您调用另一个函数将一个安排"myNotif"
在 1 小时后,它将被替换。因此,只有最晚"myNotif"
一小时后才会在pendingNotificationRequest
.