UNCalendarNotificationTrigger不会被存储,除非重复是真实的
我注意到,如果我创build一个自定义date的UNCalendarNotificationTrigger
它不会被添加,除非我把: let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: **true**)
苹果例子是:
let date = DateComponents() date.hour = 8 date.minute = 30 let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
这是有道理的重复==真实。
在我的情况下,我不需要创build一个重复多次的通知,但是我需要在特定的日历date(这当然是将来)中只触发一次的多个通知。
如果我在做:
let calendar = Calendar(identifier: .gregorian) let formatter = DateFormatter() formatter.dateFormat = "yyyyMMdd" let newdate = formatter.date(from: "20161201") let components = calendar.dateComponents(in: .current, from: newdate!) let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
那么我总是得到0等待通知…
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (notifications) in print("num of pending notifications \(notifications.count)") }) num of pending notification 0
任何想法?
编辑1:添加其中一个答案指出的其他上下文。 我实际上是将请求添加到当前的UNUserNotificationQueue。
let request = UNNotificationRequest(identifier: "future_calendar_event_\(date_yyyyMMdd)", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in if let error = error { // Do something with error print(error.localizedDescription) } else { print("adding \((request.trigger as! UNCalendarNotificationTrigger).dateComponents.date)") } }
我有同样的问题,现在我解决它。 这是由于dateComponents的一年。
为了解决这个问题,我testing了下面的代码:
1。
let notificationCenter = UNUserNotificationCenter.current() let notificationDate = Date().addingTimeInterval(TimeInterval(10)) let component = calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: notificationDate) print(component) let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false) let request = UNNotificationRequest(identifier: item.addingDate.description, content: content, trigger: trigger) self.notificationCenter.add(request){(error) in if let _ = error { assertionFailure() } }
在控制台中,打印component
:
year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false
在这种情况下,通知无法在待处理通知列表中find。
2.明确设定component
年份到2017年:
let notificationDate = Date().addingTimeInterval(TimeInterval(10)) var component = calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: notificationDate) component.year = 2017 print(component) let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false) let request = UNNotificationRequest(identifier: item.addingDate.description, content: content, trigger: trigger) self.notificationCenter.add(request){(error) in if let _ = error { assertionFailure() } }
在控制台中, component
是:
year: 2017 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false
然后这个通知可以在待处理的通知列表中find。
接下来,我检查待处理的通知请求,以查找触发date年份组件是106年还是2017年:
notificationCenter.getPendingNotificationRequests(){[unowned self] requests in for request in requests { guard let trigger = request.trigger as? UNCalendarNotificationTrigger else {return} print(self.calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: trigger.nextTriggerDate()!)) } }
我发现触发器的nextTriggerDate组件是:
year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false
结论
所以如果你想把触发器的重复设置为false,你应该确保触发date大于当前date。
默认的dateComponents的年份可能不合适,比如106.如果你想在2017年发布通知,你应该明确地设置元件年份到2017年。
也许这是一个错误,因为我将触发器的dateComponents的年份设置为2017年,但在pendingTriggerDate中获得106个未决通知请求。
UNCalendarNotificationTrigger
创build通知应该发生的时间表,但不会执行时间表。 为此,您需要创build一个UNNotificationRequest
,然后将其添加到通知中心。 有些东西是:
let request = UNNotificationRequest(identifier: "MyTrigger", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in if let error = error { // Do something with error } else { // Request was added successfully } }