Before IOS 17.0, we can update the badge by providing the badge number when schedule a notification like this:
let content = UNMutableNotificationContent()
content.title = "...."
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)
// ...
let request = UNNotificationRequest(...)
UNUserNotificationCenter.current().add(request) {...}
But now there is an warning: 'applicationIconBadgeNumber' was deprecated in iOS 17.0: Use -[UNUserNotificationCenter setBadgeCount:withCompletionHandler:] instead.
How can I update the badge number when a local notification is delivered when app running at background?
EDIT:
I have tried UNUserNotificationCenterDelegate
to update the badge number with UNUserNotificationCenter.current().setBadgeCount(notificationCount)
, but it seems can only handle both case:
userNotificationCenter(_ center:, willPresent:)
: get called the app was running in the foreground.userNotificationCenter(_ center:, didReceive:)
: get called user interact with the notification.