[英]didReceiveRemoteNotification: is not called after notification arrives and app is in background/suspended mode
在AppDelegate
我只需更新applicationIconBadgeNumber
:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
application.applicationIconBadgeNumber += 1
}
當應用程序連接到Xcode並處於調試器模式時,一切都按預期工作。 但是在我從Xcode插出之后,通知就到了,但徽章沒有更新。 應用程序處於后台模式 。
為什么? 我的做法有什么問題? 請給我一個建議🏆
推送通知由iOS而非您的應用程序處理,您無法在收到推送通知時更改應用程序徽章。 您可以在推送通知的有效負載中發送徽章編號,
有效載荷可能如下所示:
{
"aps" : {
"alert" : "Notification REceived",
"badge" : 1
}
}
您需要檢查didReceiveRemoteNotification
與fetchCompletionHandler
的UNUserNotificationCenter
委托方法
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
messagingManager?.appDidReceiveMessage(userInfo)
}
我發現這篇文章對於在后台管理通知很有幫助,基本上你必須使用帶有完成處理程序的調用。
您的messagingManager
是可選的。 你應該檢查,它不是nil
。 如果為nil
,則不會觸發appDidReciveMessage()
函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.