gpt4 book ai didi

iOS 10.2 UNUserNotificationCenterDelegate/UNTimeIntervalNotificationTrigger 错误?

转载 作者:可可西里 更新时间:2023-11-01 06:13:27 28 4
gpt4 key购买 nike

我在 iOS 10.2 中遇到了一个奇怪的错误,其中包含 UNTimeIntervalNotificationTriggerUNUserNotificationCenterDelegate。基本上,我创建的通知会立即被代表接收,然后在正确的内部再次接收。只有当触发器的 repeats 属性设置为 true 时才会发生这种情况。

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

创建通知的示例代码

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)

UNUserNotificationCenterDelegate 在.add 之后直接触发

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)
}

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

  let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)

最佳答案

我仍然没有找到根本问题,也没有看到/听到其他人遇到这个问题。与此同时,这是我的修复(尽管我希望它能正常工作)。

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

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

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

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, 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([])
}

添加和委托(delegate)调用之间的时间非常短,平均约为 0.04。

关于iOS 10.2 UNUserNotificationCenterDelegate/UNTimeIntervalNotificationTrigger 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41469726/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com