How does Facebook add badge numbers on app icon in Android?
Hi you can use this lib simply.
Support : Sony,Samsung,LG,HTC,Xiaomi,ASUS,ADW,APEX,NOVA,Huawei,ZUK,OPPO
Add :
int badgeCount = 1;ShortcutBadger.applyCount(context, badgeCount);
Remove :
ShortcutBadger.applyCount(context, 0);
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
.
Declare the
com.sonyericsson.home.permission.BROADCAST_BADGE
permission in your manifest file:Broadcast an
Intent
to theBadgeReceiver
: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);
Done. Once this
Intent
is broadcast the launcher should show a badge on your application icon.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.
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.