1

Within AppDelegate I simply update applicationIconBadgeNumber:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    application.applicationIconBadgeNumber += 1
}

Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.

Why? What is wrong with my approach? Please, give me an advice

Bartłomiej Semańczyk
  • 56,735
  • 45
  • 213
  • 327

4 Answers4

4

Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification. You can send the badge number in the payload of the push notification,

Payload could look like this:

{
    "aps" : {
        "alert" : "Notification REceived",
        "badge" : 1
    }
}
Junaid
  • 117
  • 2
  • 11
0

You need to check in didReceiveRemoteNotification with fetchCompletionHandler of UNUserNotificationCenter delegate method

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

  messagingManager?.appDidReceiveMessage(userInfo)
}
iParesh
  • 2,130
  • 1
  • 15
  • 29
0

I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.

didReceiveRemoteNotification when in background

Maria
  • 4,379
  • 1
  • 24
  • 24
-1

Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered