gpt4 book ai didi

ios - 应用程序未运行/应用程序终止时如何处理推送通知

转载 作者:行者123 更新时间:2023-11-28 14:56:49 26 4
gpt4 key购买 nike

我正在使用 Firebase 通知。当 applicationState 处于后台时,它工作正常。但是,当应用程序未运行/终止时,未调用 func getPushNotiData(_ notification: NSNotification)

在appDelegate文件中实现NotificationCenter来处理dashView中的通知

func application(_ application: UIApplication, didReceiveRemoteNotification 
userInfo: [AnyHashable : Any]) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue:
"getPushNotificationData"), object: nil, userInfo: userInfo)
}

如果应用程序未运行/终止在 didFinishLaunchingWithOptions 委托(delegate)方法中实现 NotificationCenter

 if (launchOptions != nil)
{
let userInfo = launchOptions![.remoteNotification] as? [AnyHashable: Any]
if userInfo != nil {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "getPushNotificationData"), object: nil, userInfo: userInfo)
}
}

在 dashView 中处理 NotificationCenter

override func viewDidAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(self.getPushNotiData(_:)), name: NSNotification.Name(rawValue: "getPushNotificationData"), object: nil)
}

@objc func getPushNotiData(_ notification: NSNotification){
if let url = notification.userInfo!["url"] as? String {
let destination = storyboard?.instantiateViewController(withIdentifier: "NotificationDlsViewController") as! NotificationDlsViewController
destination.notiUrl = url
self.navigationController?.pushViewController(destination, animated: true)
}
}

最佳答案

如果应用程序未运行(已终止状态),您无法执行任何方法或代码,只有当用户单击推送通知时,您才能在 didFinishLaunchingWithOptions launchingOptions 中接收它的对象并处理它....

关于ios - 应用程序未运行/应用程序终止时如何处理推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49272354/

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