iosswiftpush-notificationapple-push-notificationsbadge

How can I show an empty badge when a push notification is received on iOS?


We have an ask to show an empty badge on our iOS app icon when a push notification is received.

From what I understand, setting the badge count to -1 will show an empty badge (however this tidbit is from other posts; I couldn't find it in the official documentation).

We are already using an SDK that has a drop-in Notification Service extension. What we've tried:

  1. Catch the request object in didReceiveNotificationRequest (before its sent to the SDK's Notification Service... or so we think?). Copy the content from the request object, updated the badge value, create a new request object with the original objects identifier and trigger and the new content, and send that to the Notification Service. This crashes the notification service, resulting in a rich push being delivered as a standard push with no image.
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.baseExtension = [[ITBNotificationServiceExtension alloc] init];

    UNMutableNotificationContent *requestWithBadgeCount = request.mutableCopy;
    NSNumber *emptyBadgeCount = [NSNumber numberWithInt:-1];
    requestWithBadgeCount.badge = emptyBadgeCount;
    UNNotificationRequest *updatedRequest = [UNNotificationRequest requestWithIdentifier:request.identifier content:requestWithBadgeCount trigger:request.trigger];

    [self.baseExtension didReceiveNotificationRequest:updatedRequest withContentHandler:contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
    [self.baseExtension serviceExtensionTimeWillExpire];
}
@end
  1. Patch the SDK's Notification Service by updating the bestAttempCount.badge. This has not affect, but didn't crash the service.
  2. Patch the SDK's Notification Service by calling UNUserNotificationCenter.current().setBadgeCount() (wrapped in a guard for iOS 16+). This works, but only with positive integer. Setting -1 had no effect.

Are we barking up the wrong tree here?


Solution

  • The concept of an 'Empty Badge' does not exist in iOS.

    Though, setting the badge's value to 0 will ensure that no badge is shown.

    {
    "aps": {
        "alert": {
            "title": "Notification Title",
            "body": "Notification Body"
        },
        "badge": 0
    }
    

    }