簡體   English   中英

didReceiveRemoteNotification:在通知到達且app處於后台/暫停模式后未調用

[英]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
    }
}

您需要檢查didReceiveRemoteNotificationfetchCompletionHandlerUNUserNotificationCenter委托方法

func application(
  _ application: UIApplication,
  didReceiveRemoteNotification userInfo: [AnyHashable : Any],
  fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

  messagingManager?.appDidReceiveMessage(userInfo)
}

我發現這篇文章對於在后台管理通知很有幫助,基本上你必須使用帶有完成處理程序的調用。

在后台使用didReceiveRemoteNotification

您的messagingManager是可選的。 你應該檢查,它不是nil 如果為nil ,則不會觸發appDidReciveMessage()函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM