gpt4 book ai didi

swift - 如何知道ios本地通知何时被触发?

转载 作者:行者123 更新时间:2023-11-30 11:56:12 24 4
gpt4 key购买 nike

请告诉我如何知道 Swift 4 中何时触发本地通知?我已经尝试了所有似乎不起作用的方法。

最佳答案

您可以创建本地通知

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: true)
let content = UNMutableNotificationContent.init()
content.title = "Danger Will Robinson"
content.subtitle = "Something This Way Comes"
content.body = "I need to tell you something, but first read this."

let notification = UNUserNotificationCenter.current()
notification.add(UNNotificationRequest.init(identifier: "notification", content: content, trigger: trigger), withCompletionHandler: nil)

当通知在后台触发并且用户点击通知时。

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void)

当触发通知且应用程序位于前台时。

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void)

您必须确认 UNUserNotificationCenterDelegate 才能获取触发器。

所以最好的方法是在 appDelegate 中初始化本地通知。

关于swift - 如何知道ios本地通知何时被触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47788458/

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