gpt4 book ai didi

ios - 访问 UNNotificationRequest 对象的预定日期

转载 作者:技术小花猫 更新时间:2023-10-29 11:19:51 29 4
gpt4 key购买 nike

我试图找到 UNNotificationRequest 对象的预定触发日期。

我正在像这样获取待处理的通知请求:

UNUserNotificationCenter.current().getPendingNotificationRequests { (notifications) in

let pendingNotifications : [UNNotificationRequest] = notifications
}

然后我试图访问每个 UNNotificationRequest 对象的触发日期。

我可以按如下方式访问 UNNotificationTrigger,但找不到访问通知的预定触发日期的方法。

let notification = pendingNotifications[indexOfNotification]
let trigger : [UNNotificationTrigger] = notification.trigger

我已经能够访问一些通知的日期,如下所示:

let date = trigger.value(forKey: "date") as! Date

这适用于使用 UNUserNotificationCenter 安排的通知,但在尝试访问 iOS 10 之前安排的通知日期时出现以下错误。

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key date.'

有没有一种方法可以同时支持新旧通知。

谢谢。

最佳答案

can't find a way to access the scheduled fire date of the notification.

您已经表明您了解如何获取 UNNotificationTrigger。嗯,UNNotificationTrigger 是一个抽象父类(super class)。您需要找出它真正 是什么类并将其归结为该类。然后您可以探索它的属性。

例如:

  • 如果它是 UNCalendarNotificationTrigger,则将其转换为 UNCalendarNotificationTrigger。现在它有一个 nextTriggerDate

  • 如果它是 UNTimeIntervalNotificationTrigger,则将其转换为 UNTimeIntervalNotificationTrigger。现在它有一个 nextTriggerDate

编辑 但请注意,存在一个巨大的错误:如果这是一个 UNTimeIntervalNotificationTrigger,则 nextTriggerDate 将是错误的。

关于ios - 访问 UNNotificationRequest 对象的预定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40725136/

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