2

Despite having "remote notification" in background modes, the app does not seem to do anything when the app is in the background. All I want to do is updating the badge number when the app is closed or terminated (like what Twitter app does).

func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    UIApplication.shared.applicationIconBadgeNumber = 99 // just to test
    completionHandler(.newData)
}

Background Mode settings: enter image description here

When the app is in foreground it updates the badge, this is the payload:

{
  "aps": {
    "badge": 4,
    "content-available": 1,
    "mutable-content": 0
  }
}

But when it's in background it does nothing. Should it not update the badge?

Maysam
  • 7,246
  • 13
  • 68
  • 106
  • This might or might not be relevant: your code is wrong, because you are not calling the `completionHandler`. – matt Oct 21 '20 at 21:10
  • In the actual code I do call it, I just didn't put it here. it's either `completionHandler(.newData)` or with `.noData` or `.failed` – Maysam Oct 21 '20 at 21:16
  • OK so does anything happen at all? Does the badge change to `4`? – matt Oct 21 '20 at 21:36
  • Probably duplicate of https://stackoverflow.com/questions/14256643/update-badge-with-push-notification-while-app-in-background. You either pass `badge` in `aps` or set it in code, but not both. – matt Oct 21 '20 at 21:37
  • No nothing happens, no matter if I have or have not `UIApplication.shared.applicationIconBadgeNumber`, the badge number won't change. – Maysam Oct 21 '20 at 21:39

0 Answers0