Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a notification badge on the app icon using flutter?

Tags:

flutter

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 ?

like image 444
Kleyson Rios Avatar asked May 23 '20 23:05

Kleyson Rios


2 Answers

Late answer but, on your question i think you want to add count to the app icon just like the image below.

Notification badge on ios

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.

like image 133
Abel Tilahun Avatar answered Sep 19 '22 01:09

Abel Tilahun


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>"
}

enter image description here enter image description here

like image 45
C.K. Avatar answered Sep 21 '22 01:09

C.K.