Difference in behavior between setBadgeCount and applicationIconBadgeNumber

Hi,

I have a question regarding the badge in push notifications. I am implementing the following code to set the badge to 0 in order to remove it from the app:

swift

if #available(iOS 16, *) {
    let center = UNUserNotificationCenter.current()
    center.setBadgeCount(0)
} else {
    application.applicationIconBadgeNumber = 0
}

When I call setBadgeCount(0), the badge disappears, but the notifications in the Notification Center remain. On the other hand, when I call application.applicationIconBadgeNumber = 0, the badge disappears and all notifications in the Notification Center are cleared. Additionally, if no badge is displayed, calling application.applicationIconBadgeNumber = 0 does not clear the notifications in the Notification Center.

Is this behavior expected?

If it is expected, is there a way to call setBadgeCount(0) and clear all notifications in the Notification Center? Also, is there a way to call application.applicationIconBadgeNumber = 0 without clearing the notifications in the Notification Center?

Answered by Engineer in 789670022

With the new UNUserNotificationCenter APIs to control your badge and notifications, the functionalities are split.

setBadgeCount() now only changes the application badge number, and does not remove notifications from the Notification Center.

To remove the notifications you can use removeAllDeliveredNotifications() or its more fine combed cousin removeDeliveredNotifications(withIdentifiers:)

With the new UNUserNotificationCenter APIs to control your badge and notifications, the functionalities are split.

setBadgeCount() now only changes the application badge number, and does not remove notifications from the Notification Center.

To remove the notifications you can use removeAllDeliveredNotifications() or its more fine combed cousin removeDeliveredNotifications(withIdentifiers:)

Thank you for your response.

Is my understanding correct that if the application has a badge and there are notifications in the notification center, calling application.applicationIconBadgeNumber = 0 will clear all notifications in the notification center, but if the application does not have a badge and there are notifications in the notification center, calling application.applicationIconBadgeNumber = 0 will not clear the notifications in the notification center?

Also, is there a way to set the badge to 0 while keeping the notifications in the notification center if the application has a badge and there are notifications in the notification center?

Difference in behavior between setBadgeCount and applicationIconBadgeNumber
 
 
Q