On Android one does nothing special. It automatically counts the number of unhandled notifications you have. Although there is the issue which is already reported regarding not being able to call method to remove all delivered notifications at once: https://github.com/ionic-team/capacitor/issues/1217.
But for iOS, what is the thought here? How does one achieve what is already working out of the box on Android?
iOS differs from Android with regards to the badge-count. While on Android it is directly connected to the number of unhandled recieved notifications on iOS it's dependent upon what you yourself set it to.
You need to include the badge-count in the payload:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9
}
}
What I have done for my usecase is the following:
On my backend whenever a push-notification is to be sent I do a count for unread messages and then send the push-notification with the count as the badge in the payload.
When the app opens I clear all notifications (which also clears the badge-count):
PushNotifications.removeAllDeliveredNotifications()
When app is in foreground I don't take into consideration any recieved notification - whether it is alert, sound or badge. One does this in Capacitor by adding the following in capacitor.config.json under "plugins":
"PushNotifications": {
"presentationOptions": []
}
I feel like there still needs to be a way to handle modifying the badge count. In the 3 steps you mentioned, I agree with Step 1 and 3, but if there are pending alerts that the user should see then I want the app badge to just decrement appropriately instead of clearing everything away. If I do clear everything away then the user won't see the pending alerts until they get another badge related notification.
I would love to see methods like getBadgeCount setBadgeCount that I can use with removeDeliveredNotifications and removeAllDelivereNotifications so that I can update the badge count exactly as I want.
Most helpful comment
I feel like there still needs to be a way to handle modifying the badge count. In the 3 steps you mentioned, I agree with Step 1 and 3, but if there are pending alerts that the user should see then I want the app badge to just decrement appropriately instead of clearing everything away. If I do clear everything away then the user won't see the pending alerts until they get another badge related notification.
I would love to see methods like
getBadgeCountsetBadgeCountthat I can use withremoveDeliveredNotificationsandremoveAllDelivereNotificationsso that I can update the badge count exactly as I want.