IOS 应用程序关闭时的通知
IOS notification when Application is closed
我有一个 ios 应用程序,当从我设置的服务器发送通知时我向其发送远程通知 content -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")
}
}
那么即使应用程序已关闭,我也应该将此方法更改为 运行
谢谢
didReceiveRemoteNotification方法不会在应用程序关闭时调用。
但是您可以在 appdelegate 的 didFinishLaunchingWithOptions 方法中检查 launchOptions 以了解应用程序是否已通过通知启动相应的任务。
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)
}
}
我有一个 ios 应用程序,当从我设置的服务器发送通知时我向其发送远程通知 content -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")
}
}
那么即使应用程序已关闭,我也应该将此方法更改为 运行
谢谢
didReceiveRemoteNotification方法不会在应用程序关闭时调用。
但是您可以在 appdelegate 的 didFinishLaunchingWithOptions 方法中检查 launchOptions 以了解应用程序是否已通过通知启动相应的任务。
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)
}
}