Android update app badge count without push notification Android Oreo+

Issue

I want to update app icon badge count without push notifications(eg. Silent push). I simply want to update app badge count after reading any notifications inside the app similar to linkedIn, where its updating the count on app badge when am reading any notification. Currently I’ve implemented the solution where I am having payload from FCM with badge count and updating it with android notification builder with silent push notification channel configured.

var notificationBuilder  NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
    .setContentTitle("New Messages")
    .setContentText("You've received 3 new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
    .setNumber(messageCount)
    .build()

but that is not gonna work for me as I also need to update the count once I’ve read any notification inside the app. If I clear the notification from notification tray then count is also disappearing from app badge, is there any way that I can set that count without push notification within the app? Thanks in advance.

Solution

So after working around badge count update feature so long, I’ve come to conclusion that Android do not provide any api to update badge count on app icon without notification. In android we can internally send notification and with the help of this we can update badge count.

So we need to notify the device for updating badge count, without notifying we can’t update it. If we are sending count to Notification Builder then we just need to take care to update badge count to keep the notification id same every time if we want to update the badge count otherwise it will add count to its previous notification. Just keep notification id same and create a separate channel with silent push to update badge count.

public static int NOTIFICATION_ID  1001; 
NotificationManager notificationManager 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

that solution will override previous count.

Answered By – Ammy Kang

Leave a Comment