关闭应用程序时的iOS通知

San*_*yap 5 apn push-notification ios remote-notifications

我有一个ios应用程序,从服务器发送通知时我将远程通知发送到哪里,我正在设置内容-available = 1声音=“” 在IOS设备上使用时

applicaiton(didReceiveRemoteNotification:fetchCompletionHandler :)

我看到应用程序在后台运行时到达此方法,但是在关闭应用程序时未调用此方法,但是在关闭应用程序时未调用该方法

applicaiton(didReceiveRemoteNotification:fetchCompletionHandler :)

我在此方法中的处理程序是

        let userInfoDic = userInfo as NSDictionary as? [String: Any]
        let cato = userInfoDic?["cato"] as? String
if cato == "message" {
            let state: UIApplicationState = UIApplication.shared.applicationState
            if state == .active{
                print("Application is Active Socket IO will Handle This")
            }else{
                let apnMessage = userInfoDic?["apnMessage"] as? [String: Any]
                if (apnMessage == nil){
                    print("Error Fetching Message")
                }else{
                let count = UserDefaults.standard.integer(forKey:"TotalUnredMessage")

                    DispatchQueue.main.async {
                        UIApplication.shared.applicationIconBadgeNumber = count + 1
                    }
                    UserDefaults.standard.set(count, forKey: "TotalUnredMessage")
          }
}
Run Code Online (Sandbox Code Playgroud)

因此,即使关闭了应用程序,我也应该更改此方法才能运行

谢谢

Pra*_*iva 6

当应用程序关闭时,didReceiveRemoteNotification方法将不会调用。

但是您可以在appdelegate的didFinishWithLaunchOptions方法中检查launchOptions以了解天气是否已从通知启动应用程序,并相应地执行任务。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

     if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
    // Do your task here
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 如果用户没有通过点击通知打开应用程序,则无法获取通知信息。在这种情况下,您必须检查您的服务器是否有未读通知。 (2认同)