Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change app notification badge count programmatically without sending notification (java/android)

Tags:

java

android

I want to change the notification badge count every time a user arrives at the home page (or presses a button for testing purposes). The only way I can do that right now is by sending a notification like so:

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

However, I want to change the badge count without sending a notification as I don't want to clutter up the notification panel.

like image 468
Dobby Doo Avatar asked Aug 06 '26 16:08

Dobby Doo


1 Answers

Welcome to StackOverflow.

It would appear the pacakge you're using is no longer maintained, as it's been deprecated in favour of AndroidX and I'd recommend migrating to that if it's an option for your project.

If I'm correct in my assumption, you're attempting to do something similar to what you can achieve on iOS, however the Android SDK does not support this out of the box, although there appears to be a workaround

As such, the function you're calling cannot be used for that particular purpose. The setNumber function sets the number displayed in the long press menu

All this having been said

You CAN update a notification that's already been sent, and update the number shown in the long press menu using the setNumber method, as detailed in this article

TL;DR:

  • Post the notification with an identifier using the following method and save the identifier somewhere for later: NotificationManagerCompat.notify(notificationId, builder.build());

  • Rerun the same code you posted in your question, updating the badge number in the process

  • Run NotificationManagerCompat.notify() again, passing the SAME notification id and the NEW notification.

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

int notificationID = 123456;
int messageCount = 1;

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

notificationManager.notify(notificationID, notification);

//Now update the message count
messageCount++;

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

notificationManager.notify(notificationID, notification);
like image 130
Will Jones Avatar answered Aug 09 '26 07:08

Will Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!