4

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
busuu
  • 586
  • 9
  • 22
  • Check this if it helps- https://stackoverflow.com/questions/42193987/how-to-auto-increment-ios-notification-badge-with-firebase-cloud-messaging – Pranav Jan 12 '22 at 16:50
  • 1
    @Pranav so the only solution would be to create my own mechanism that will store the current badge count for every user in my own database, and then manipulate with that data? – busuu Jan 12 '22 at 18:09
  • Yup, I believe so. – Pranav Jan 12 '22 at 18:15
  • busuu - I also have similar use case to solve. There is a way in iOS to handle this - https://pushpsenairekar.medium.com/increment-ios-apps-badge-count-in-using-5-simple-steps-3d80e77d45c8 but I am trying to do it in flutter so I dont have to manage it separately. let me know which approach you implemented. – Aniket Kalamkar Dec 30 '22 at 21:55
  • @AniketKalamkar i implemented my own logic from scratch. At the time of posting this question there was no library that can do this, but i think someone was in the process of making a notification library that covers this major problem out of the box in flutter – busuu Jan 09 '23 at 17:57

0 Answers0