I'm using FCM to push notifications in a flutter app.
I've tried a lot of things and codes to show a badge (red dot) on top of the app icon when a new notification arrives and the app is closed or in background.
What should I do to get a badge app icon in flutter ?
Late answer but, on your question i think you want to add count to the app icon just like the image below.
So for this issue you can use flutter_app_badger. Using this package you can add count to your app icon.
To use flutter_app_badger with FCM you can use this
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
//Here you can add the count when you get a notification you can increase the number by one
FlutterAppBadger.updateBadgeCount(1);
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
//Here you can remove the badge you created when it's launched
FlutterAppBadger.removeBadge();
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
Then you can also add it to the background notification handler
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
//Here you can add
FlutterAppBadger.updateBadgeCount(1);
...
// Or do other work.
}
Remember, On ios background handler is triggered when only there is no notification on the payload. You can read my more about this issue on my answer.
Try this JSON Body, I got the badge count and the sound, still trying to figure out why, and how to clear it after read.
{
"notification": {
"body": "This is an FCM notification message!",
"title": "FCM Message",
"sound": "alert.aiff"
},
"priority": "high",
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done"
},
"apns": {
"payload": {
"aps": {
"badge": 9
},
"messageID" : "ABCDEFGHIJ"
}
},
"to": "<the token you want to push to>"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With