I have a server that sends me push notifications and let\'s say that I have 5 notifications on my phone. If I open one of them all other notifications disappears. I want onl
The solution is to set the applicationIconBadgeNumber
to the real number of notifications the user has in the notification center. I made a function for this:
func updateIconBadge() {
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
DispatchQueue.main.async {
UIApplication.shared.applicationIconBadgeNumber = notifications.count
}
}
}
For more info (like where to call to this function) you may want to check my original answer here: https://stackoverflow.com/a/54296486/8157190
If you use OneSignal SDK in your app, and you have the problem of disappearing notifications after opening one, you must add key OneSignal_disable_badge_clearing
to the Info.plist
file in Xcode, as a Boolean type set to YES
.
More info here https://documentation.onesignal.com/docs/badges
By setting the applicationIconBadgeNumber
to 0
, you also remove every notification from the notification center.
This has also been discussed here: iOS application: how to clear notifications?
Furthermore, it is not possible to programmatically remove a single notification, but from iOS8 on, the OS will handle this for you when a user taps a single notification. This has also been discussed here: Remove single remote notification from Notification Center