除非重复为真,否则不会存储UNCalendarNotificationTrigger [英] UNCalendarNotificationTrigger doesn't get stored unless repeats is true
问题描述
我注意到如果我创建一个带有自定义日期的 UNCalendarNotificationTrigger
,除非我这样做,否则它不会被添加:
let trigger = UNCalendarNotificationTrigger(dateMatching:components,重复:** true **)
I've noticed that if I create an UNCalendarNotificationTrigger
with a custom date it does't get added unless i put:
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: **true**)
Apple示例是:
let date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
有意义重复== true。
which make sense to be repeats == true.
在我的场景中,我不需要创建一个重复多次的通知,但我需要在特定的日历日期(当然是将来)只发送一次多个通知..
In my scenario I dont need to create one notification that gets repeated many times, but I need multiple notificaitons fired only once on a specific calendar date (which is of course in the future)..
如果我在做:
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待处理的通知......
then i always get 0 pending notifications...
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (notifications) in
print("num of pending notifications \(notifications.count)")
})
num of pending notification 0
有什么想法吗?
EDIT1:
添加其中一个答案指出的其他上下文。
我实际上是将请求添加到当前的UNUserNotificationQueue。
Adding other context as pointed out by one of the answers. I'm actually adding the request to the current 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的年份。
I have the same problem and I solve it now. It is due to dateComponents' year.
为了解决这个问题,我测试了以下代码:
In order to solve this problem, I test the following code:
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()
}
}
在控制台中,打印组件
:
year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false
在这种情况下,在待处理通知列表中找不到通知。
And in this case, the notification cannot be found in pending notification list.
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()
}
}
在控制台中,组件
是:
year: 2017 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false
然后可以在pe中找到此通知通知清单。
Then this notification can be found in pending notification list.
然后,我检查待处理的通知请求,以查找触发日期的年份组件是106还是2017:
And next, I check in the pending notification requests to find whether the trigger-date's year component is 106 or 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组件是:
I find the trigger's nextTriggerDate components are:
year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false
结论
因此,如果您想将触发器的重复设置为false,应确保触发日期大于当前日期。
默认的dateComponents年份可能不合适,例如106.如果您希望通知2017年火灾,你应该明确地将组件年份设置为2017年。
The default dateComponents' year may be unsuitable, such as 106. If you want the notification to fire in 2017, you should set the components year to 2017 explicitly.
也许这是一个错误,因为我将触发器的dateComponents'年设置为2017年,但在nextTriggerDate的待处理通知请求中得到106.
Perhaps this is a bug, because I set trigger's dateComponents' year to 2017 but get 106 in nextTriggerDate of pending notification request.
这篇关于除非重复为真,否则不会存储UNCalendarNotificationTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!