应用程序关闭时的IOS通知 [英] IOS notification when Application is closed

查看:509
本文介绍了应用程序关闭时的IOS通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ios应用程序我从服务器发送通知时向我发送远程通知我设置 content -available = 1 sound =
在我使用的IOS设备上

i have a ios application where to which i send remote notification when sending notification from server i am setting content -available = 1 and sound = "" When on IOS device i am using


应用程序(didReceiveRemoteNotification:fetchCompletionHandler:)

applicaiton(didReceiveRemoteNotification:fetchCompletionHandler:)

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

I seeing that app reaches this method when in background but when app is closed this method is not getting called But is not getting called when application is closed


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")
          }
}

那我应该改变什么呢?即使应用程序关闭也要运行此方法

So what should i change for this methods to run even when applicaiton is closed

谢谢

推荐答案

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

didReceiveRemoteNotification method will not call when the application is closed.

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

But you can check launchOptions to know weather the application has been launched from notification or not in didFinishWithLaunchOptions method in appdelegate and do your task accordingly.

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

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

}

这篇关于应用程序关闭时的IOS通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆