【发布时间】:2017-07-19 14:09:13
【问题描述】:
我使用以下代码触发每日提醒:
let triggerDate = calendar.date(from: calendarComponents)
let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: triggerDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "daily-identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
//UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
if error != nil {
debugPrint("center.add(request, withCompletionHandler: { (error)")
} else {
debugPrint("no error?! at request added place")
}
})
上面的代码允许我设置一个标识符为daily-identifier的每日闹钟。因为我打算在我的代码中设置两个每日闹钟,所以我使用上面的代码并使用另一个标识符second-daily-identifier 来触发另一个具有不同小时/分钟组合的闹钟。
我遇到的问题: 如果我使用代码
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
在添加请求之前,它将删除之前设置的警报(例如,如果我先添加“每日标识符”警报,那么当我添加“第二个每日标识符”时将删除“每日标识符”警报.
如果我使用其他方法
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
要专门删除我要设置的那个,然后效果很好。然而,我发现我再也无法阻止它了。例如,如果我将标识符更改为“第三标识符”,那么已经设置的闹钟将永远存在于我的手机中。即使我删除了应用程序,我也无法删除它们。
我做错了什么?
【问题讨论】:
-
是的,即使您卸载然后重新安装,该通知仍将存在,并带有先前设置的标识符。因此,仅当您删除了所有具有匹配标识符的现有通知时才更改标识符。这可能有点痛苦,苹果应该让它删除所有通知。
-
我真的很惊讶,在我删除应用程序后,它仍然在这里!
标签: ios swift notifications unusernotificationcenter