gpt4 book ai didi

ios - 如果应用程序是通过本地通知启动的,则访问通知负载

转载 作者:行者123 更新时间:2023-11-29 05:23:03 25 4
gpt4 key购买 nike

如果用户在应用程序处于挂起状态时单击本地通知并启动应用程序,我如何才能收到通知并访问通知负载?每当我的 iOS 应用程序从后台状态打开时,都会调用 UNNotification 响应函数。通过按键启动的本地通知已被弃用,我不知道如何确定应用程序是否已通过本地通知启动。我需要使用通知负载来正确填充 View Controller 。

我使用此函数在应用程序处于后台状态时获取通知响应:

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

setup view here using payload
}

最佳答案

问题可能是您没有尽早配置 UNUserNotificationCenterDelegate。您需要尽早执行此操作,如 didFinishLaunching 中所示,以便运行时知道将 didReceive 发送到何处。我就是这样做的:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self.notifHelper
return true
}

关于ios - 如果应用程序是通过本地通知启动的,则访问通知负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469882/

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