应用程序关闭时的IOS通知

时间:2017-07-12 12:31:10

标签: ios push-notification apn remote-notifications

我有一个ios应用程序,当我从服务器发送通知时,我在哪里发送远程通知我设置 content -available = 1 sound =&#34;&#34; < /强> 在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")
          }
}

那么,即使关闭应用程序,我应该更改此方法的运行

由于

3 个答案:

答案 0 :(得分:5)

应用程序关闭时,

didReceiveRemoteNotification 方法不会调用。

但您可以查看 launchOptions ,了解应用程序是否已通知appnlegate中的 didFinishWithLaunchOptions 方法通知,并相应地执行您的任务。

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

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

}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

通过显示如何在 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)
        }
}