setting badge count when app is killed

Hi, I have an app that receives push notification from server with "content-available": 1


and i have set my app to increase badge count as soon as it receives push notifications like

func application(
        _ application: UIApplication,
        didReceiveRemoteNotification userInfo: [AnyHashable : Any],
        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
    ) {
          let badgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1       
          UIApplication.shared.applicationIconBadgeNumber = badgeNumber
}


so my app shows badge count correctly in any state (active, background, killed) when it is built in Debug.


But my app is not showing badge count when app is in killed state and it is built in Released.

(if it is in background mode, badge shows correctly)


Is there anyway that I can control badge count from app side (not sending "badge" property through push notification)?


All I need to do is, update badge number as soon as app receives push notifications


Thanks in advance. Let me know if need more information

Replies

Since push notifications are handled by iOS and not your app you can't change the application badge on receiving a push notification.

But you can send the badge number in the payload of the push notification, but then you will have to do the calculation server side.

You should read Local and Push Notification Programming Guide and especially Notification Payload.


The payload could look like this:

{

"aps" : {

"alert" : "You got your emails.",

"badge" : 9

}

}

Now the app application badge icon will show 9.

  • So, in any case, the server should send a badge number as silent data. Cause, the callback will not be triggered until the user taps the notification.

Add a Comment