Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

Modify a Notification Badge

Starting with 8.0 (API level 26), notification badges (also known as notification dots) appear on a launcher icon when the associated app has an active notification. Users can long-press on the app icon to reveal the notifications (alongside any app shortcuts), as shown in figure 1.

These dots appear by default in launcher apps that support them and there's nothing your app needs to do. However, there might be situations in which you don't want the to notification dot to appear or you want to control exactly which notifications to appear there.

Figure 1. Notification badges and the long-press menu

Disable badging

There are cases where badges don't make sense for your notifications, so you can disable them on a per-channel basis by calling setShowBadge(false) on your NotificationChannel object.

For example, you might want to disable notification badges in the following situations:

  • Ongoing notifications: Most ongoing notifications, such as image processing, media playback controls, or current navigation instructions, don't make sense as a badge.
  • Calendar reminders: Avoid badging events occurring at the current time.
  • Clock or alarm events: Avoid badging notifications related to current alarms.

The following sample code illustrates how to hide badges for a notification channel:

String id = "my_channel_01";
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.setShowBadge(false);

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);

Set custom notification count

By default, each notification increments a number displayed on the long- press menu (visible in figure 1), but you can override this number for your app. For example, this might be useful if you're using just one notification to represent multiple new messages but you want the count here to represent the number of total new messages.

To set a custom number, call setNumber() on the notification, as shown here:

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();

Modify a notification's long-press menu icon

The long-press menu displays the large or small icon associated with a notification if available. By default, the system displays the large icon, but you can call Notification.Builder.setBadgeIconType() and pass in the BADGE_ICON_SMALL constant to display the small icon.

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)
        .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
        .build();

Hide a duplicate shortcut

If your app creates a notification that duplicates an app shortcut, you can temporarily hide the shortcut while the notification is active by calling setShortcutId().