0

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
  • 59,234
  • 49
  • 233
  • 358

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

I think the best way to manage badge count is on the server. A push notification will automatically update the badge on the app icon.

Payload formate.

{
    "aps" : {
        "alert" : "Weather report udapted",
        "badge" : 5
    }
}
iParesh
  • 2,338
  • 1
  • 18
  • 30
  • I updated the question. My delegate method is not called when app is in background mode and not connected to the debugger;) – Bartłomiej Semańczyk Nov 16 '18 at 07:17
  • Have you confirm delegate UNUserNotificationCenter.current().delegate = self inside application(_:didFinishLaunchingWithOptions:) method – iParesh Nov 16 '18 at 07:18
  • Yes, I can confirm;) That method is called when app is connected to the debugger, but doesnt work when not connected to the debugger. Why? – Bartłomiej Semańczyk Nov 16 '18 at 07:21
  • What makes the difference with that two methods?;) – Bartłomiej Semańczyk Nov 16 '18 at 07:33
  • Hello @BartłomiejSemańczyk my didreceiveremotenotification method is called when app is put in background by content available 1. but after some time the method stops getting called no matter how many push i send. can you PLEASE HELP. – Ambesh Tiwari Aug 06 '22 at 10:36
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,471
  • 1
  • 25
  • 26
-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