How to set badge count in Oreo without showing a notification?

Viewed 6002

I used to show a number in app icon using this library as follows:

ShortcutBadger.applyCount(context, numberToShow);

OneSignal also has same function in its Android SDK.

Now in Oreo, with the introduction of notification channels, things get complex to me. I can create a channel. Then, I can also create a notification as follows:

public static void createNotification(Context context, int numberToShow) {
    Notification notification = new NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id))
            .setContentTitle("Dummy Title")
            .setContentText("Dummy content")
            .setSmallIcon(R.drawable.app_icon)
            .setNumber(numberToShow)
            .build();
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(0, notification);
}

However, I have to show a notification with this solution, which I don't need and thus don't want. Is there any way in Oreo that I can achieve the same thing I have done previously, i.e. just showing 'notification dot' or a number attached to the app icon?

2 Answers

Sorry, but there is no SDK-level support for showing numbers or other badges on launcher icons, other than the Notification scenario that you described.

set the importance of the notification channel to

IMPORTANCE_MIN

like int importance = NotificationManager.IMPORTANCE_MIN; and then create the channel as -

NotificationChannel nChannel = new NotificationChannel
                    (channelId, title, importance);

This will the set the badge count(shown on the long press of the icon) without notifying the user about any notification in the system tray. Though the notification will be in the tray, but will not pop up and quietly reside there.

Related