9

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

应用程序(didReceiveRemoteNotification:fetchCompletionHandler :)

我看到该应用程序在后台时到达此方法,但是当应用程序关闭时,此方法没有被调用但是在应用程序关闭时没有被调用

应用程序(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")
          }
}

那么即使应用程序关闭,我应该改变什么才能运行此方法

谢谢

4

3 回答 3

10

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

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

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

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

}
于 2017-07-12T12:36:27.933 回答
1

通过展示如何检索通知 userIn didFinishWithLaunchOptions来完成 Prakash Shaiva

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

     if let launchOptions = launchOptions,
            let userInfo =  launchOptions[.remoteNotification] as? [AnyHashable: Any] {
            //some code here and post when needed like below
            NotificationCenter.default.post(name: NSNotification.Name("your notification name"),  object: nil)
        }
}
于 2020-06-02T14:52:56.863 回答
0

您应该在配置中指定您的应用需要在远程通知时唤醒。检查此评论:https ://stackoverflow.com/a/29231395/2732896

于 2017-07-12T12:39:11.087 回答