How to Interface with the Badgeprovider on Samsung Phones to Add a Count to the App Icon

How to interface with the BadgeProvider on Samsung phones to add a count to the app icon?

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

How can i show the badge count on app icon in android? ShortcutBadger does not work, I don't want to use widget

you could generate the local notification with custom count.

example :

Android Doc

Kotlin:

var notification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setNumber(3)
.build()

Java:

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(3)
.build();

following will trigger the notification.

NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());

Android - how to add a badge count to an application icon?

It is currently not possible to target "all android devices", only for some.

Certain manufacturers (e.g. Samsung notably) have included this functionality into their customised Android launchers. Also some 3rd-party launchers (e.g. Nova Launcher) have included an API to accomplish this.

Stock Android does not offer this functionality at the moment on the standard launcher.


I have just seen that this is a duplicate of:

  • Is there a way to add a badge to an application icon in Android?

There are many other related posts about this type of thing:

  • How does Facebook add badge numbers on app icon in Android?
  • https://stackoverflow.com/questions/18205569/does-samsung-modifies-its-android-roms-to-have-badges-on-email-and-sms-icons?rq=1
  • adding notification badge on app icon in android
  • How to interface with the BadgeProvider on Samsung phones to add a count to the app icon?
  • How to add a notification badge/count to application icon on Sony Xperia devices?
  • How to make application badge on android?
  • How to display count of notifications in app launcher icon

You can find more information there.

How to show notification count on app icon like Facebook?

Unfortunately you cant achieve this for all android devices.

Certain manufacturers (e.g. Samsung or Sony) have included this functionality into their customised Android launchers. Also some 3rd-party launchers (e.g. Nova Launcher) have included an API to accomplish this.

Some related posts for more information:

  • How does Facebook add badge numbers on app icon in Android?
  • https://stackoverflow.com/questions/18205569/does-samsung-modifies-its-android-roms-to-have-badges-on-email-and-sms-icons?rq=1
  • adding notification badge on app icon in android
  • How to interface with the BadgeProvider on Samsung phones to add a count to the app icon?
  • How to add a notification badge/count to application icon on Sony Xperia devices?
  • How to make application badge on android?
  • How to display count of notifications in app launcher icon

And some libraries that might be helpful:

  • Samsung badger

  • ShortcutBadger

  • Badges

and more...

Is there a way to add a badge to an application icon in Android?

Unfortunately, Android does not allow changing of the application icon because it's sealed in the APK once the program is compiled. There is no way to programmatically change it to a 'drawable'.

You may achieve your goal by using a widget instead of an icon. Widgets are highly customisable and can do what you want.

There's a short discussion about the difference between iPhone icon notification and using widgets here:

http://www.cnet.com/8301-19736_1-10278814-251.html

As you'll notice, there is virtually no difference between using a widget or an icon, since they can be the same size and look the same.

How to display count of notifications in app launcher icon

Android ("vanilla" android without custom launchers and touch interfaces) does not allow changing of the application icon, because it is sealed in the .apk tightly once the program is compiled. There is no way to change it to a 'drawable' programmatically using standard APIs. You may achieve your goal by using a widget instead of an icon. Widgets are customisable. Please read this :http://www.cnet.com/8301-19736_1-10278814-251.html and this http://developer.android.com/guide/topics/appwidgets/index.html.
Also look here: https://github.com/jgilfelt/android-viewbadger. It can help you.

As for badge numbers. As I said before - there is no standard way for doing this. But we all know that Android is an open operating system and we can do everything we want with it, so the only way to add a badge number - is either to use some 3-rd party apps or custom launchers, or front-end touch interfaces: Samsung TouchWiz or Sony Xperia's interface. Other answers use this capabilities and you can search for this on stackoverflow, e.g. here. But I will repeat one more time: there is no standard API for this and I want to say it is a bad practice. App's icon notification badge is an iOS pattern and it should not be used in Android apps anyway. In Andrioid there is a status bar notifications for these purposes:http://developer.android.com/guide/topics/ui/notifiers/notifications.html
So, if Facebook or someone other use this - it is not a common pattern or trend we should consider. But if you insist anyway and don't want to use home screen widgets then look here, please:

How does Facebook add badge numbers on app icon in Android?

As you see this is not an actual Facebook app it's TouchWiz. In vanilla android this can be achieved with Nova Launcher http://forums.androidcentral.com/android-applications/199709-how-guide-global-badge-notifications.html
So if you will see icon badges somewhere, be sure it is either a 3-rd party launcher or touch interface (frontend wrapper). May be sometime Google will add this capability to the standard Android API.

How to add a notification badge/count to application icon on Sony Xperia devices?

After having seen Daniel Ochoa's solution for Samsung's launcher, which uses a BadgeProvider to handle the badges, I set out to do the same for Sony's Xperia Home. This answer is taken directly from my blog.

How I figured it out - For anyone interested

I stumbled upon Sony's AppXplore and used it to check out the permission's of the Facebook app.
The Facebook app requests the following permission, which is the key to displaying badges on Sony devices:

com.sonyericsson.home.permission.BROADCAST_BADGE

Next, I had a look through all available content providers but I found nothing related to app icon badges there.
I ran the command in this answer to get a system dump file and searched for "badge" using Notepad++. I found this:

com.sonyericsson.home.action.UPDATE_BADGE:
41be9a90 com.sonyericsson.home/.BadgeService$BadgeReceiver filter 41be9858

So, it's handled using a BroadcastReciever on Sony as opposed to Samsung's Content Provider. So, I created a dummy BroadcastReciever of my own, listening for the action com.sonyericsson.home.action.UPDATE_BADGE, and found the extras passed to Sony's BadgeService.
For this, I also needed a permission, but that was easy to find in the dump file:

com.sonyericsson.home.permission.RECEIVE_BADGE

The extras sent by Facebook, the Email app, etc, are:

  • com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME - The
    name of your app's main activity, android.intent.action.MAIN. This is
    so the launcher knows which icon to show the badge on.
  • com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE - a
    boolean indicating if we want to show the badge or not (which we do!)
  • com.sonyericsson.home.intent.extra.badge.MESSAGE - a string
    (not an integer - that took me a while to realize...) with the number
    to show.
  • com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME - The name of your application package.

How to show badges on your app's launcher icon on Sony Xperia devices

So, it turns out it's very simple to show a badge on your application icon in the launcher. IMO it's much more straight-forward than for Samsung's launcher.
Here's a step-by-step-guide (and it's not long!)

  1. Declare the com.sonyericsson.home.permission.BROADCAST_BADGE permission in your manifest file:

  2. Broadcast an Intent to the BadgeReceiver:

    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);
  3. Done. Once this Intent is broadcast the launcher should show a badge on your application icon.

  4. To remove the badge again, simply send a new broadcast, this time with SHOW_MESSAGE set to false:

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

Good to know

The message is a string!

Since MESSAGE is a String, you can actually add words to the badge:

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

But I wouldn't do that 'cause it just looks weird.

You have access to all apps!

The BROADCAST_BADGE permission does not only give you access to your own app's icon, but to ALL of them. For example, here's how you can set Facebook's badge:

Intent intent = new Intent();
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.facebook.katana.LoginActivity");
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.facebook.katana");

sendBroadcast(intent);

I hope this has been of help to someone! :)



Related Topics



Leave a reply



Submit