nextTriggerDate() 没有 return 'expected' 值,是否有另一种方法来获取重复本地通知的下一个触发日期?
nextTriggerDate() doesn't return the 'expected' value, is there another way to obtain the next fire date of a repeating Local Notification?
在我的应用程序中,我允许用户安排重复的本地通知。我遇到的问题(许多其他基于环顾四周的问题)是 nextTriggerDate() 始终将其 return 值基于当前时间而不是安排通知的时间。我看到了在通知的 userInfo 中存储 "date" 值的建议,但看到通知是如何重复的,它似乎无法在每次通知触发时保持此日期值准确。有什么方法可以获取重复本地通知的实际触发日期?
func getNotifications(completion: @escaping (Bool)->()){
notificationArray.removeAll()
center.getPendingNotificationRequests { (notifications) in
print("Count: \(notifications.count)")
if notifications.count <= 0{
completion(true)
return
}
for item in notifications {
var nextTrigger = Date()
if let trigger = item.trigger as? UNTimeIntervalNotificationTrigger{
nextTrigger = trigger.nextTriggerDate()!
}else if let trigger = item.trigger as? UNCalendarNotificationTrigger{
nextTrigger = trigger.nextTriggerDate()!
}
}
}
completion(true)
}
确认。我运行这个代码:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)
print("scheduling at", Date())
DispatchQueue.main.asyncAfter(deadline: .now()+15) {
print("checking at", Date())
UNUserNotificationCenter.current().getPendingNotificationRequests {
arr in let arr = arr
if let req = arr[0].trigger as? UNTimeIntervalNotificationTrigger {
let fd = req.nextTriggerDate()
print("trigger date", fd as Any)
}
}
}
// ... proceed to configure and schedule the notification
我还配置了我的用户通知中心代表在前台接收通知并打印时间。
这是我在控制台中看到的内容:
scheduling at 2018-08-01 03:40:36 +0000
checking at 2018-08-01 03:40:51 +0000
trigger date Optional(2018-08-01 03:42:51 +0000)
received notification while active 2018-08-01 03:42:36 +0000
所以当我检查时,触发日期被报告为从开始的 2 分钟,但通知实际上是从我安排的 2 分钟后触发的].
我会把它描述为一个错误!
我对你原来的问题的一个不同意见是,对于 非 重复的 UNTimeIntervalNotificationTrigger:
我得到了完全相同的结果
scheduling at 2018-08-01 03:45:50 +0000
checking at 2018-08-01 03:46:06 +0000
trigger date Optional(2018-08-01 03:48:06 +0000)
received notification while active 2018-08-01 03:47:50 +0000
UNTimeIntervalNotificationTrigger 也有一个 timeInterval
属性,但即使这样对我们也没有好处,除非我们知道最初安排通知的时间 — 而 UNNotificationRequest 无法找到它。
(为了确定,我推迟了检查,直到重复通知触发了几次,但结果是一样的:nextTriggerDate
显然是将 timeInterval
添加到 now 而不是报告下一次触发通知的时间。)
在我的应用程序中,我允许用户安排重复的本地通知。我遇到的问题(许多其他基于环顾四周的问题)是 nextTriggerDate() 始终将其 return 值基于当前时间而不是安排通知的时间。我看到了在通知的 userInfo 中存储 "date" 值的建议,但看到通知是如何重复的,它似乎无法在每次通知触发时保持此日期值准确。有什么方法可以获取重复本地通知的实际触发日期?
func getNotifications(completion: @escaping (Bool)->()){
notificationArray.removeAll()
center.getPendingNotificationRequests { (notifications) in
print("Count: \(notifications.count)")
if notifications.count <= 0{
completion(true)
return
}
for item in notifications {
var nextTrigger = Date()
if let trigger = item.trigger as? UNTimeIntervalNotificationTrigger{
nextTrigger = trigger.nextTriggerDate()!
}else if let trigger = item.trigger as? UNCalendarNotificationTrigger{
nextTrigger = trigger.nextTriggerDate()!
}
}
}
completion(true)
}
确认。我运行这个代码:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)
print("scheduling at", Date())
DispatchQueue.main.asyncAfter(deadline: .now()+15) {
print("checking at", Date())
UNUserNotificationCenter.current().getPendingNotificationRequests {
arr in let arr = arr
if let req = arr[0].trigger as? UNTimeIntervalNotificationTrigger {
let fd = req.nextTriggerDate()
print("trigger date", fd as Any)
}
}
}
// ... proceed to configure and schedule the notification
我还配置了我的用户通知中心代表在前台接收通知并打印时间。
这是我在控制台中看到的内容:
scheduling at 2018-08-01 03:40:36 +0000
checking at 2018-08-01 03:40:51 +0000
trigger date Optional(2018-08-01 03:42:51 +0000)
received notification while active 2018-08-01 03:42:36 +0000
所以当我检查时,触发日期被报告为从开始的 2 分钟,但通知实际上是从我安排的 2 分钟后触发的].
我会把它描述为一个错误!
我对你原来的问题的一个不同意见是,对于 非 重复的 UNTimeIntervalNotificationTrigger:
我得到了完全相同的结果scheduling at 2018-08-01 03:45:50 +0000
checking at 2018-08-01 03:46:06 +0000
trigger date Optional(2018-08-01 03:48:06 +0000)
received notification while active 2018-08-01 03:47:50 +0000
UNTimeIntervalNotificationTrigger 也有一个 timeInterval
属性,但即使这样对我们也没有好处,除非我们知道最初安排通知的时间 — 而 UNNotificationRequest 无法找到它。
(为了确定,我推迟了检查,直到重复通知触发了几次,但结果是一样的:nextTriggerDate
显然是将 timeInterval
添加到 now 而不是报告下一次触发通知的时间。)