简体   繁体   中英

Setting iOS app badge while in the background after receiving a remote notification

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM