Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification Badges not woking in Android O

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);
like image 441
Aniket Thakur Avatar asked Apr 02 '17 17:04

Aniket Thakur


People also ask

What is app icon badges on Android?

The app icon badge tells you there's a new notification or message waiting for you when you open the app, and the badge count tells you how many notifications there are.

What happens when you turn off notification badges?

When notification badges are disabled, notifications will still be shown in the status bar — the only difference is that no badge will be shown on the application icon. When using notification badges, it’s important to remember that you don’t need to badge every type of notification from your app.

What is a notification badge on a launcher?

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.

Why am I not receiving notifications on my Android?

There are many different causes for not receiving notifications on your Android. Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.

What types of notifications should I avoid badging?

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.


1 Answers

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.

like image 143
blizzard Avatar answered Oct 22 '22 02:10

blizzard