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?
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:)