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

JavaAndroidFacebookBadgeAndroid Launcher

Java Problem Overview


I know there are several Qs here that ask if its possible to add badges to an android app and they all end up with a NO answer...

But somehow the latest Facebook beta version for Android seems to do something which at least look like a badge even if it is not technically exactly that.

In that post one of the commenters says that it is somehow related to TouchWiz. And also here they mention it as a feature of the "S3 TouchWiz Jelly Bean Addon".

I still would appreciate information on how does this can be done and if there is some API for this that I can use in my own app (when running in an appropriate environment - i.e. the same device where FB demonstrates this behavior) ?

Java Solutions


Solution 1 - Java

Hi you can use this lib simply.

Support : Sony,Samsung,LG,HTC,Xiaomi,ASUS,ADW,APEX,NOVA,Huawei,ZUK,OPPO

ShortcutBadger

enter image description here

Add :

int badgeCount = 1;
ShortcutBadger.applyCount(context, badgeCount);  

Remove :

ShortcutBadger.applyCount(context, 0);

Solution 2 - Java

I have figured out how this is done for Sony devices.

I've blogged about it here. I've also posted a seperate SO question about this here.


Sony devices use a class named BadgeReciever.

  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);
    

I've excluded details on how I found this to keep the answer short, but it's all available in the blog. Might be an interesting read for someone.

Solution 3 - Java

There is not a standard way to achieve this; Many makers such as Sony or Samsung have implemented it in their own Android customization.

For example in Samsung, you have to broadcast an intent with BADGE_COUNT_UPDATE action, let MainActivity be your main activity class and count be the number you want to display in your app icon, note that 0 will hide the badge:

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", MainActivity.class.getName());
context.sendBroadcast(intent);

Sony devices uses "com.sonyericsson.home.action.UPDATE_BADGE" action with their custom extras as @Marcus Answered, so you have to add "com.sonyericsson.home.permission.BROADCAST_BADGE" permission to your app manifest and:

Intent intent = new Intent("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", MainActivity.class.getName());
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);

Note: it's desirable to query your app's data (context.getPackageName(), MainActivity.class.getName()) rather than hardcode it just in case you do some refactoring in the future.

Solution 4 - Java

> But somehow the latest Facebook beta version for android does just that...

Not according to the forum thread that contains the screenshot that you linked to. Quoting vakama94:

> Well, that's actually TouchWiz and not just the app. I have a Galaxy S II running JellyBean 4.1.2 and it makes the same thing but with some other applications

Whether Samsung has a public API to allow apps to publish numbers to be used as badges, I cannot say. This could be something that they did privately with a few firms.

You are welcome to provide evidence of seeing these badges on a stock Android home screen, such as one of the Nexus series devices.

Solution 5 - Java

I answer to this assuming that some flutter dev can search this... In Flutter, you can achieve this by using Flutter app badger library.

It is as simple as

FlutterAppBadger.updateBadgeCount(1); // show
FlutterAppBadger.removeBadge(); // hide

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionepelegView Question on Stackoverflow
Solution 1 - JavaAhmad AghazadehView Answer on Stackoverflow
Solution 2 - JavaMarcusView Answer on Stackoverflow
Solution 3 - JavaJorge ArimanyView Answer on Stackoverflow
Solution 4 - JavaCommonsWareView Answer on Stackoverflow
Solution 5 - JavaK.AmanovView Answer on Stackoverflow