Ios 应用未运行/应用终止时如何处理推送通知
我正在使用firebase通知。当应用程序状态为后台时,它工作正常。但当应用程序未运行/终止时,不会调用func getPushNotiData(u通知:NSNotification) 在appDelegate文件中实现NotificationCenter以在dashView中处理通知Ios 应用未运行/应用终止时如何处理推送通知,ios,swift,push-notification,Ios,Swift,Push Notification,我正在使用firebase通知。当应用程序状态为后台时,它工作正常。但当应用程序未运行/终止时,不会调用func getPushNotiData(u通知:NSNotification) 在appDelegate文件中实现NotificationCenter以在dashView中处理通知 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any]) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue:
"getPushNotificationData"), object: nil, userInfo: userInfo)
}
如果应用程序未运行/终止,则在didFinishLaunchingWithOptions委托方法中实现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)
}
}
如果应用程序未运行(已终止状态),则无法执行任何方法或代码,只有当用户单击推送通知时,您才能在
didfinitishlaunchingwithoptions
launchingOptions中接收推送通知的对象并对其进行处理……当您在didfinitishlaunchingwithoptions
中选中launchingwithoptions
时,您的视图控制器可能尚未加载,因此viewdid出现未调用,并且未在通知中心注册ViewController。当应用程序从通知启动时,尝试放入一些打印语句并检查调用顺序。当应用程序未运行/终止/处于终止状态时,推送通知到达时,将不会执行任何方法。系统将只显示一个通知弹出窗口。@当应用程序未运行时,objc func getPushNotiData(Notification:NSNotification)方法未执行