0
votes

I'm trying to update the badge value remotely via a push notification. Everything works fine but when the app is open I get an empty alert (containing my app name and a close button).

I read about silent notifications in apple's documentation but it seems that even with specifying content-available : 1 in the notification payload, the payload must not contain badge for the notification to be silent.

Is updating the badge without receiving an alert possible ?

1

1 Answers

0
votes

If you are using a silent push notification, then you cannot include the badge key in the aps dictionary, so you can't update the badge directly with a silent push.

You can include your own keys and values in the aps dictionary and this dictionary is available in the didReceiveRemoteNotification:fetchCompletionHandler UIApplicationDelegate function.

In this function you can extract your own "badge" key from the aps dictionary and use this to update the badge number directly using the UIApplication property [applicationIconBadgeNumber][1]

UIApplication.shared.applicationIconBadgeNumber = someValue

Something like:

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if let badgeNumberStr = userInfo["myBadge"] as? String {
        if let badgeNumber = Int(badgeNumberStr) {
            application.applicationIconBadgeNumber = badgeNumber
        }
    }
}