>我有一个iOS应用程序,当从服务器发送通知时,我向其发送远程通知,我正在设置内容 -可用= 1和声音=">在我使用的 IOS 设备上时
application(didReceiveRemoteNotification:fetchCompletionHandler:(
我看到应用程序在后台达到此方法,但是当应用程序关闭时,此方法未被调用,但在应用程序关闭时未被调用
application(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")
}
}
那么即使应用程序关闭,我应该更改什么才能运行此方法
谢谢
didReceiveRemoteNotification方法在应用程序关闭时不会调用。
但是您可以检查启动选项以了解应用程序是否已从通知启动的天气 appdelegate 中的didFinishLaunchingWithOptions方法并相应地执行您的任务。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
// Do your task here
}
}
通过显示如何检索通知用户来完成 Prakash Shaiva 在didFinishWithLaunchOptions中
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)
}
}
应在配置中指定应用需要在远程通知时唤醒。 检查此评论:https://stackoverflow.com/a/29231395/2732896