0
我用下面的代碼來觸發每天提醒:每日提醒在iOS
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()
,它會刪除設置較早的(例如,如果我添加「每日標識」報警,然後再報警時我添加「第二日用標識符」將刪除「日用idenifier」報警。
如果我使用其他方法
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
特異性除去我要設定的那個,那麼它運作良好。但是,我發現我再也無法阻止它了。例如,如果我已將標識符更改爲「第三標識符」,則已設置的鬧鐘將永久存在於我的電話中。即使我刪除了應用程序,我也無法刪除它們。
我做錯了什麼?
是即使卸載後重新安裝該通知將仍與先前存在設置標識符。因此,只有當您使用匹配的標識符移除所有現有通知時,才更改標識符。這可能有點痛苦,蘋果應該只是刪除所有通知。 – zsteed
我真的很驚訝,我刪除了應用程序後,它仍然在這裏! – user6539552