1

I've seen a bunch of questions about getting the correct iOS app badge number in response to a remote notification (like this, this, and this). They all hinge on realizing that the OS will badge the app with the number in the payload before your app does anything. I get that, but my question is a bit different.

Assume the following series of events for an app that's enabled the "remote-notification" background mode (and otherwise properly configured for remote notifications):

  1. The server sends a remote notification. The payload says the badge number should be 7.
  2. The OS gets this, badges the app with "7", shows the alert/banner to the user, and launches/wakes my app into the background to start processing data.
  3. Shortly after running, the app realizes the server is wrong and the correct badge number should be 3.

Maybe no big deal if the user comes into the app right away, but I'd like the badge to be correct if they don't. Can I set the badge number from within application:didReceiveRemoteNotification:fetchCompletionHandler: while executing in the background?

This article suggests you can change the badge if you asked for the "background fetch" background mode and the OS granted your wish. Any reason it shouldn't work if you're backgrounded to handle a notification?

Community
  • 1
  • 1
Cem Schemel
  • 442
  • 3
  • 13
  • 1
    you can set application badge [UIApplication sharedApplication].applicationIconBadgeNumber = 1; like this programatically if you have the correct badge count – HardikDG Feb 19 '16 at 03:43
  • Thanks; I'm aware of that. But can I do that while in the background, while processing a notification, and have it replace the badge number that the notification set (if any)? – Cem Schemel Feb 19 '16 at 05:48

1 Answers1

0

For anyone who stumbles upon this later: I managed to get everything setup to actually try this out. Yes, you can mess with the app icon badge while in the background to handle a remote notification. At least when it comes to the badge, there's nothing special or restrictive about executing in the background due to a notification as opposed to any other background mode.

Cem Schemel
  • 442
  • 3
  • 13