In a chat app, I am sending messages between two users. I'm using firebase to send notifications and they work fine in foreground and background states, even in terminated states i'm still receiving the notification.
However the notification count badge on iOS devices is not updating when i send it while the app has been terminated. I get the notification – but the badge count does not increase, because none of the code seems to get executed in .onBackgroundMessage()
.
I am sending this payload:
{
"notification": {
"title": "New message",
"body": "Notification sent from POSTMAN",
"sound": "custom_sound.mp3",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"icon": "app_icon"
},
"to": "abcd...",
"content_available": true,
"priority": "high"
}
And after googling i came across the solution for this to just add "badge": 2
inside the "notification"
field. The value of badge can be any number and that number actually gets updated on the target device when the notification is sent.
{
"notification": {
"title": "New message",
"body": "Notification sent from POSTMAN",
"sound": "custom_sound.mp3",
"badge": 2,
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"icon": "app_icon"
},
"to": "abcd...",
"content_available": true,
"priority": "high"
}
However here's the problem: how am i supposed to know what number to pass in the badge field?
- Lets say A sends a message to B with badge count of 1.
- B receives that notification and increases badge count to 1 automatically.
After this, lets say C wants to send a message to B, how is C supposed to know what is the current notification badge count of B, so that it can add just +1 to it, and increase the total badge count to 2?