6

With Android O developer preview google has introduced notification badges that are supposed to be shown on launcher icon. I am using emulator with Android O from dev channel.I wrote a simple code to show notification badge but it does not seem to work -

        Notification notification = new Notification.Builder(getApplicationContext())
                .chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
                .setSmallIcon(android.R.drawable.btn_star)
                .setNumber(10)
                .build();

        mNotificationManager.notify(1, notification);

It just shows as normal notification.

API - https://developer.android.com/reference/android/app/Notification.Builder.html#chooseBadgeIcon(int)

Has anyone worked on this yet? Am I missing something?

Show badge is enabled in settings.

enter image description here


Tried with NotificationChannel too. Does not work -

    NotificationChannel mChannel = new NotificationChannel("TestBadge_id", "TestBadgeName", NotificationManager.IMPORTANCE_HIGH);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    mChannel.setShowBadge(true);
    mNotificationManager.createNotificationChannel(mChannel);


    Notification notification = new Notification.Builder(getApplicationContext())
                .chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
                .setSmallIcon(android.R.drawable.btn_star)
                .setNumber(10)
            .setChannel("TestBadge_id")
                .build();

        mNotificationManager.notify(1, notification);
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • I cannot get this working on a Nexus 5X with the firmware images, even on a channel in which I opted into badges. My guess is that this is not implemented yet. – CommonsWare Apr 02 '17 at 17:27
  • @CommonsWare is there a way with code to opt into badges that I am missing? I can try that if you could share a code snippet. – Aniket Thakur Apr 02 '17 at 17:29
  • I'm pretty sure you need a compatible launcher to make this work end-to-end. Being that there aren't any launchers that support this yet then you many not see the effects of this. – Shmuel Apr 02 '17 at 17:47
  • @AniketThakur: am referring to `setShowBadge(true)` on `NotificationChannel`, then use that channel when creating the `Notification`. – CommonsWare Apr 02 '17 at 17:49
  • @Shmuel: I would hope that the launcher supplied with the Google APIs emulator and the hardware firmware builds would eventually support badges. I am surprised that they do not seem to do so already. – CommonsWare Apr 02 '17 at 17:49
  • @CommonsWare agreed. But this is the situation it would seem. Frankly aosp launcher doesn't really matter to Google. They have the Google Now/ Pixel Launcher. I'm sure those will have support for icon badges eventually, possibly even before the aosp launcher – Shmuel Apr 02 '17 at 17:52
  • @Shmuel: "Frankly aosp launcher doesn't really matter to Google" -- AFAIK, that is now only on the non-Google APIs emulators. The Google APIs emulator (Android 7.0+) use some variation on the Pixel Launcher (round icons, etc.). – CommonsWare Apr 02 '17 at 18:05

1 Answers1

7

The Notification badge examples of Android-O doesn't seem to work in emulator in early preview releases. But with the latest release of Android-O developer preview-3 the badges are displayed properly as documented in the Notification Badges section.

To display notification badge, you need to set the setShowBadge(boolean) for a notification channel to true. By default badges will be displayed like below:

Sample image to showcase badges

Upon long press if there is a more than a one notification the count is displayed.The count automatically increments/decrements based on the active notifications. You can also adjust the count manually by using the Notification.Builder.setNumber().

Sample showing notification count on long press of launcher icon:

Badge notification showing count

Make sure you are targetting the latest API:

compileSdkVersion 26 
buildToolsVersion "26.0.0"
targetSdkVersion 26

Tested in pixel Android emulator version 26.1.1.

blizzard
  • 5,275
  • 2
  • 34
  • 48
  • Following bizzard's answer, I am able to see Notification badge in emulator with Pixel Android API 26 configuration. However, I neither specify [setShowBadge(boolean)](https://developer.android.com/reference/android/app/NotificationChannel.html#setShowBadge(boolean)) for a notification channel to true nor use [Notification.Builder.setNumber()](https://developer.android.com/reference/android/app/Notification.Builder.html#setNumber(int)) – Chris Li Jul 05 '17 at 12:27