ios - nextTriggerDate() 不返回 'expected' 值,是否有其他方法来获取重复本地通知的下一个触发日期?

标签 ios swift cocoapods uilocalnotification

在我的应用程序中,我允许用户安排重复的本地通知。我遇到的问题(基于环顾四周的任何其他问题)是 nextTriggerDate() 始终将其返回值基于当前时间而不是安排通知的时间。我看到了在通知的 userInfo 中存储“日期”值的建议,但看到通知如何重复,似乎每次通知触发时都无法保持该日期值准确。有没有办法获取重复本地通知的实际触发日期?

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现在,而不是报告下次触发通知的时间。)

关于ios - nextTriggerDate() 不返回 'expected' 值,是否有其他方法来获取重复本地通知的下一个触发日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51618620/

相关文章:

ios - 在 UIButton 中垂直显示标题?

ios - 类 X 在 <framework> 和 <application> 中都实现了,将使用两者之一,哪个未定义

ios - 使用 cachePolicy 出错

ios - 在 Win 8 中用于 iOS 开发的 Swift

ios - 在主要目标中包含 pod,而不是在 WatchKit 扩展中

ios - UISearchController 需要额外点击才能成为第一响应者

ios - Swift 从 URL 获取所有项目

ios - TableView 的 UISearchController

ios - 切换到调试时体系结构 x86_64 的 undefined symbol

static-libraries - Cocoapods --no-integrate 针对不同目标的命令