iOS 10.2 UNUserNotificationCenterDelegateUNTimeIntervalNotificationTrigger错误?

iOS 10.2 UNUserNotificationCenterDelegateUNTimeIntervalNotificationTrigger错误?,第1张

概述我遇到了一个带有UNTimeIntervalNotificationTrigger和UNUserNotificationCenterDelegate的iOS 10.2的奇怪错误.基本上我创建的通知会立即由代表接收,然后再在正确的内部接收.仅当触发器上的repeats属性设置为true时才会发生这种情况. 还有其他人看过这个问题吗?现在我想我需要检查委托中的触发日期并与存储的注册日期进行比较 – 但 我遇到了一个带有UNTimeIntervalNotificationTrigger和UNUserNotificationCenterDelegate的iOS 10.2的奇怪错误.基本上我创建的通知会立即由代表接收,然后再在正确的内部接收.仅当触发器上的repeats属性设置为true时才会发生这种情况.

还有其他人看过这个问题吗?现在我想我需要检查委托中的触发日期并与存储的注册日期进行比较 – 但我想尽可能避免这种情况.

用于创建通知的示例代码

let content = UNMutableNotificationContent()content.body = "My notification message"let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,repeats: true)let request = UNNotificationRequest(IDentifIEr: "MY_IDENTIFIER",content: content,trigger: trigger)UNUserNotificationCenter.current().add(request,withCompletionHandler: nil)

在.add之后直接触发UNUserNotificationCenterDelegate

func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaPing (UNNotificationPresentationoptions) -> VoID) {      // Code called here instantly when trigger repeats = true      // Code called again at proper interval as well (60 seconds)}

如果我将触发器更改为重复false,则不会发生这种情况

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60,repeats: false)
解决方法 我仍然没有找到根本问题或者已经看到/听到过其他人的问题.与此同时,这是我的修复(尽管我希望它能正常工作).

我创建了一个字典来存储通知被触发的时间.我使用通知标识符作为密钥:

scheduledTimes[IDentifIEr] = CACurrentMediaTime()UNUserNotificationCenter.current().add(request,withCompletionHandler: nil)

然后在委托中,我可以将它与当前时间进行比较,看看我是否应该忽略它:

func userNotificationCenter(_ center: UNUserNotificationCenter,withCompletionHandler completionHandler: @escaPing (UNNotificationPresentationoptions) -> VoID) {    let IDentifIEr = notification.request.IDentifIEr    // required for 10.2?    // Adding a notification request to the notification center immediately fires this delegate when the trigger is set to repeat    if let scheduledTime = scheduledTimes[IDentifIEr] {        if CACurrentMediaTime() - scheduledTime < 1.0 {            completionHandler([])            return        }    }    // Parse the notification into an internal notification and show it    completionHandler([])}

添加和委托调用之间的时间非常短,平均为0.04.

总结

以上是内存溢出为你收集整理的iOS 10.2 UNUserNotificationCenterDelegate / UNTimeIntervalNotificationTrigger错误?全部内容,希望文章能够帮你解决iOS 10.2 UNUserNotificationCenterDelegate / UNTimeIntervalNotificationTrigger错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1002303.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存