ios 退出程序通知后台_应用程序关闭时的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

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:)

My handler in this method is

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

Thanks

解决方案

didReceiveRemoteNotification method will not call when the application is closed.

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值