37

Samsung's TWLauncher allows apps to create badge counts on app icons.

This is completely undocumented! There is no mention of it anywhere, and only a handful of apps are using it (e.g. Facebook, eBay).

How do you use this functionality to add a count to your app icon?

This is very specific to Samsung devices. I am not asking about Android in general. I'm only asking about badging Samsung's Touchwhiz interface which currently allows badging. Android does not.

Daniel Ochoa
  • 1,783
  • 1
  • 16
  • 20
  • I believe you meant "undocumented EVERYWHERE". Unless you meant "documented NOWHERE". Undocumented ANYWHERE??? What does that mean? – Samir Mar 21 '16 at 08:54
  • @sammyg Good point and made the change. Basically just saying when I wrote this there was no documentation available on the internet for how to interface with the BadgeProvider. – Daniel Ochoa Mar 22 '16 at 15:48
  • Possible duplicate of [Is there a way to add a badge to an application icon in Android?](https://stackoverflow.com/questions/2905542/is-there-a-way-to-add-a-badge-to-an-application-icon-in-android) – Shirish Herwade Nov 14 '17 at 11:09
  • @ShirishHerwade The link you referenced gave no answer. I did. – Daniel Ochoa Nov 15 '17 at 14:04
  • @DanielOchoa then why in first place you asked another question when there is already one present. This flag is about 'duplication question' ; not about duplicate answer or better answer. Also you can add your answer under that question, then close(delete) this question yourself and help make stackoverflow a better place – Shirish Herwade Nov 15 '17 at 14:09
  • @ShirishHerwade Because when I did a search for interfacing with Samsung's badge provider no questions popped up. That link asks for badging ANDROID. My post is not about Android generically. It is specific and only targeted at Samsung's Touchwhiz interface. It is a very clear and targeted question. That link does not answer or ask specifically about Samsung's devices. That's why I'm not going to post my answer on that link. My question/answer do not work for anything but Samsung devices. – Daniel Ochoa Nov 16 '17 at 14:50

7 Answers7

69

First you'll need to add the following permissions to your AndroidManifest.xml file.

<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

The column structure is as follows:

(integer) _id, (text) package, (text) class, (integer) badgecount, (blob) icon, (???) extraData

In order to query ALL results from the BadgeProvider do the following:

// This is the content uri for the BadgeProvider
Uri uri = Uri.parse("content://com.sec.badge/apps");

Cursor c = getContentResolver().query(uri, null, null, null, null);

// This indicates the provider doesn't exist and you probably aren't running
// on a Samsung phone running TWLauncher. This has to be outside of try/finally block
if (c == null) {
    return;
}

try {
    if (!c.moveToFirst()) {
        // No results. Nothing to query
        return;
    }

    c.moveToPosition(-1);
    while (c.moveToNext()) {
        String pkg = c.getString(1);
        String clazz = c.getString(2);
        int badgeCount = c.getInt(3);
        Log.d("BadgeTest", "package: " + pkg + ", class: " + clazz + ", count: " + String.valueOf(cnt));
    }
} finally {
    c.close();
}

In order to add a badge count to your application icon

ContentValues cv = new ContentValues();
cv.put("package", getPackageName());
// Name of your activity declared in the manifest as android.intent.action.MAIN.
// Must be fully qualified name as shown below
cv.put("class", "com.example.badge.activity.Test");
cv.put("badgecount", 1); // integer count you want to display

// Execute insert
getContentResolver().insert(Uri.parse("content://com.sec.badge/apps"), cv);

If you want to clear the badge count on your icon

ContentValues cv = new ContentValues();
cv.put("badgecount", 0);
getContentResolver().update(Uri.parse("content://com.sec.badge/apps"), cv, "package=?", new String[] {getPackageName()});  

NEW
I have created an open source project that you can import as a library to assist with this. It's licensed as Apache so feel free to use it as you please.

You can get it from here: https://github.com/shafty023/SamsungBadger

Wladimir Palant
  • 53,866
  • 11
  • 93
  • 123
Daniel Ochoa
  • 1,783
  • 1
  • 16
  • 20
  • could you please you more elaborate... ? – Akarsh M Nov 22 '13 at 07:29
  • Yes this is exactly what Facebook does to badge their icon – Daniel Ochoa Nov 22 '13 at 16:30
  • I'll check this and revert you back about the status. And thanks for this solution . :) – Akarsh M Nov 23 '13 at 05:21
  • 2
    I created an open source library for this. You can get it here: https://github.com/shafty023/SamsungBadger – Daniel Ochoa Nov 25 '13 at 16:49
  • Thanks Daniel for sharing this :) – Damien R. Nov 27 '13 at 15:05
  • Great find! For those interested, this can also be done for Sony's Xperia Home: http://stackoverflow.com/questions/20216806/how-to-add-a-notification-badge-count-to-application-icon-on-sony-xperia-devices/ – Marcus Nov 28 '13 at 15:46
  • @Marcus Nice job figuring it out for Sony's launcher. Any clue what's the earliest version of Android that is supported on? I might change my project name from SamsungBadger to AndroidBadger and incorporate your findings into a single library. That way it's a drop in to any project and abstracts away using it on any device. – Daniel Ochoa Nov 28 '13 at 16:10
  • @Daniel Ochoa thanks! I don't know what versions that support it but since it's a broadcast at least it shouldn't cause issues on unsupported devices, right? I had plans to create a generic library myself but if you wanna do it, go ahead :) – Marcus Nov 28 '13 at 16:41
  • It would also be cool to know if HTC supports this... Anyone? – Marcus Nov 28 '13 at 16:48
  • @marcus htc supports it as well we just need someone running sense to figure it out. And yes since it's a broadcast receiver it's safe – Daniel Ochoa Nov 28 '13 at 20:28
  • I'll create a generic library for it. I'll see if i can group it into my existing project – Daniel Ochoa Nov 28 '13 at 20:31
  • This does not work on Galaxy S4. I cannot find any content providers on the phone with badge in the URI. – Vivek Bansal Jan 13 '14 at 12:33
  • @VivekBansal I use this on my Galaxy S4. Are you running a custom ROM on your galaxy s4? – Daniel Ochoa Jan 24 '14 at 21:52
  • @Daniel Ochoa Ochoa Is there any configuration need to use this library?? In my Nexus 4 isBadgingSupported method returns false because of shared preference returns 0 value. – Solution Jan 27 '14 at 09:31
  • @AnilSavaj Ah, yes this only works on devices running Samsung's TouchWhiz interface. Your Nexus 4 is a Google Experience device and thus does not support badging. Icon badging is something the Samsung TWLauncher added support for but the stock Launcher (ie: home screen app) does not support – Daniel Ochoa Jan 27 '14 at 18:28
  • Gives me: 02-02 21:48:43.494: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.exa.birthdayrem.MainActivity}: java.lang.IllegalArgumentException: Unknown URL content://com.sec.badge/apps – Skynet Feb 02 '14 at 16:29
  • @NunChai That means you are running this on a device that is not running Samsung's TouchWhiz interface – Daniel Ochoa Feb 05 '14 at 19:27
  • 1
    Yes I tested on a Samsung Device and it works flawless, wonder how Facebook manages to do it on every device. – Skynet Feb 06 '14 at 05:39
  • Works on Samsung Galaxy S2, S3, S4 and Samsung Galaxy Note 3 – Vincent Ducastel Feb 18 '14 at 14:47
  • thanks for your effeort but i cannot make it work on samsung tablets (galaxy tab 1 & 2) i see the badges in the logs but not on the application icon – Mark Mar 03 '14 at 09:15
  • 1
    Can it show a character or string like, say a star '*' instead of a counter which is an integer? – Vikram Gupta May 05 '14 at 07:25
  • not working on every devices. So is there any way to achieve this task to every devices ? @DanielOchoa – duggu Jun 03 '14 at 06:36
  • Badges appear only once ... not updating. Also not able to add badges after clearing badges from over app icon. Can you please guide on this. – AndroidHacker Oct 10 '14 at 13:45
  • 2
    @DanielOchoa I am getting this error while using the code Caused by: java.lang.SecurityException: Permission Denial: writing com.sec.android.provider.badge.BadgeProvider uri content://com.sec.badge/apps from pid=5325, uid=10242 requires com.sec.android.provider.badge.permission.WRITE, or grantUriPermission() Please help me to solve this – Shanto George Feb 23 '17 at 06:50
  • This no longer works on Samsung S8 (i.e Oreo and above). We need a new solution. – strangetimes Nov 20 '18 at 23:53
21

There is another cool open source library that support different devices: https://github.com/leolin310148/ShortcutBadger/

Vlad Yarovyi
  • 699
  • 8
  • 16
17

add these permissions to manifest

<!--for android badge-->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/>

<!--for Samsung badge-->
<uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>

<!--for htc badge-->
<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT"/>

<!--for sony badge-->
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>

<!--for apex badge-->
<uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>

add these package names to your class :

    final String HOME_PACKAGE_SONY = "com.sonyericsson.home";
    final String HOME_PACKAGE_SAMSUNG = "com.sec.android.app.launcher";
    final String HOME_PACKAGE_LG = "com.lge.launcher2";
    final String HOME_PACKAGE_HTC = "com.htc.launcher";
    final String HOME_PACKAGE_ANDROID = "com.android.launcher";
    final String HOME_PACKAGE_APEX = "com.anddoes.launcher";
    final String HOME_PACKAGE_ADW = "org.adw.launcher";
    final String HOME_PACKAGE_ADW_EX = "org.adwfreak.launcher";
    final String HOME_PACKAGE_NOVA = "com.teslacoilsw.launcher";

for use :

  // just put your pachage and main activity class path
  String classPath = "ir.faasaa.resa.MainActivity";

           ContentValues cv = new ContentValues();
                    cv.put("package", context.getPackageName());
                    cv.put("class", classPath);
                    cv.put("badgecount", count);
           context.getContentResolver().insert(Uri.parse(HOME_PACKAGE_SAMSUNG), cv);

thanks to ShortcutBadger

Hamidreza Sadegh
  • 1,980
  • 26
  • 31
8

In android, we don't have badge style approach as iOS but some manufactures are supporting to display badge on app icons.

Support for sony and HTC.

Example for badge style icon on app in android

Sony and HTC supports adding badge to the app icon.

For Sony

<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />  

Intent intent= new Intent("com.sonyericsson.home.action.UPDATE_BADGE");

intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", Class Name);

intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);

intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE",number);

intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", packageName);

sendBroadcast(intent);

for HTC:

<uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />  
 <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" />  

Intent updateIntent = new Intent("com.htc.launcher.action.UPDATE_SHORTCUT");
updateIntent.putExtra("packagename", packageName);
updateIntent.putExtra("count", number);
this.sendBroadcast(updateIntent);

Intent setNotificationIntent = new Intent("com.htc.launcher.action.SET_NOTIFICATION");
ComponentName localComponentName = new ComponentName(packageName, className);
setNotificationIntent.putExtra("com.htc.launcher.extra.COMPONENT", localComponentName.flattenToShortString());
setNotificationIntent.putExtra("com.htc.launcher.extra.COUNT", number);
this.sendBroadcast(setNotificationIntent);
Harsha Vardhan
  • 3,286
  • 2
  • 15
  • 22
5

I use this class for Samsung and Sony devices (also available https://gist.github.com/Tadas44/cdae2f5995f21bf1c27f). Don't forget to add <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" /> to AndroidManifest.xml

public class BadgeUtils {


    public static void setBadge(Context context, int count) {
        setBadgeSamsung(context, count);
        setBadgeSony(context, count);
    }

    public static void clearBadge(Context context) {
        setBadgeSamsung(context, 0);
        clearBadgeSony(context);
    }


    private static void setBadgeSamsung(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }
        Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", count);
        intent.putExtra("badge_count_package_name", context.getPackageName());
        intent.putExtra("badge_count_class_name", launcherClassName);
        context.sendBroadcast(intent);
    }

    private static void setBadgeSony(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

        context.sendBroadcast(intent);
    }


    private static void clearBadgeSony(Context context) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(0));
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

        context.sendBroadcast(intent);
    }

    private static String getLauncherClassName(Context context) {

        PackageManager pm = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                String className = resolveInfo.activityInfo.name;
                return className;
            }
        }
        return null;
    }
}
Tadas Valaitis
  • 870
  • 10
  • 11
0

There is another tool that you can use for Xperia devices

Intent intent = new Intent();

intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");

sendBroadcast(intent);



intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "10");

More details as below

http://www.marcusforsberg.net/blog/android-notification-badge-app-icon-sony/

Ahmed Salem
  • 1,527
  • 20
  • 26
0

For Android 8.0 Oreo Users-

For those who are getting trouble in showing badge count for Samsung S8, S9 or Pixel devices having Oreo in them note that ShortcutBadger Library does not supports API>26 for Samsung Reported here in issues. Here you have to check it first if isBadgeCounterSupported like that -

public boolean isBadgeCounterSupported() {
    // Workaround for bug in ShortcutBadger in version 1.1.19, registered as
    // https://github.com/leolin310148/ShortcutBadger/issues/266
    if (Build.MANUFACTURER.equalsIgnoreCase("Samsung") && Build.VERSION.SDK_INT >= 26) {
        Log.d("LOG_TAG", "Launcher icon badge (ShortcutBadger) is not supported on Samsung devices running Android 8 (or newer).");
        return false;
    }

    return ShortcutBadger.isBadgeCounterSupported(context);
}

The only way is to apply Badges from Notification. As for applying badges from your notifications, see the Android documentation, and note the section "Set custom notification count".

Notification notification = new NotificationCompat.Builder(MainActivity.this, "my_channel_01")
    .setContentTitle("New Messages")
    .setContentText("You've received 3 new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
    .setNumber(messageCount) //this badge count
    .build();

enter image description here

For more on Notifications in Android 8.0 refer to this answer .

karanatwal.github.io
  • 2,880
  • 1
  • 19
  • 51
  • The issue is, there are still several apps that are able to show red badge counts on icons, so this isn't a viable solution. We need to figure out how these apps are managing to set badge counts. – strangetimes Nov 20 '18 at 23:54
  • Any reference to app please? – karanatwal.github.io Nov 21 '18 at 11:19
  • Twitter on Samsung S8. I figured out that the only way it does that is probably as you describe above, but unfortunately requires a notification to be posted alongside. In the past you could set the badge count separately which was good. – strangetimes Nov 21 '18 at 11:46
  • Yes there are some changes over the time and now from Oreo there are dots badges which are officially supported in android. https://medium.com/cr8resume/notification-in-android-8-0-oreo-implementing-notification-channels-d65b0f81ca50 Hope to get numeric badges in future soon – karanatwal.github.io Nov 21 '18 at 12:54