Ios – Update badge with push notification while app in background

apple-push-notificationsios

I have got push notification working and managed to update icon badge count when app is brought to foreground.

I am a bit confused about this though…the iPhone receives the notification and the pop up message appears to activate my app, and the badge only updates after me starting the app.

This does not sound right in terms of user experience. My understanding is that the badge count should notify the user of what needs action, through incremented count, but this doe not happen until a later stage when the app is live.

So is there a way to tell the app to update its badge count when it receives push notifications and whilst being in the background?

Note that my app does not use location and that I have UIRemoteNotificationTypeBadge in the notification registration request.

Cheers
AF

Best Answer

Since push notification 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 the you will have to do the calculation server side.

You should read Local and Push Notification Programming Guide and especially the The 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.