2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dobby Doo
  • 21
  • 2

1 Answers1

2

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);
Will Jones
  • 1,861
  • 13
  • 24
  • 2
    Thank you for your answer.It seems to be very odd that Android SDK does not support this out of the box. The notification badge count gets automatically cleared when user launches the app. I'm writing an app similar to Whatsapp which should display number of unread message as badge count. I want to retain the badge count if the user has launched the app, but not read the messages. I'm sure this is a common use case. Why would developers be expected to write this massive workaround code where you have to cater for each manufacturer? What's the reasoning behind Android not supporting it? – Dobby Doo Nov 25 '19 at 10:36
  • I couldn't profess to know. If you want to get around that you could potentially reset the notification count to the value you want it to be on app launch. This may require subclassing the main App class, although it's something I tend to do as a matter of course during Android development anyway, as the majority of projects I work on tend to require some sort of central functionality callable by all classes/activities. – Will Jones Nov 25 '19 at 12:03