Adding Notification Badge On App Icon In Android
Solution 1:
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
publicclassBadgeUtils {
publicstaticvoidsetBadge(Context context, int count) {
setBadgeSamsung(context, count);
setBadgeSony(context, count);
}
publicstaticvoidclearBadge(Context context) {
setBadgeSamsung(context, 0);
clearBadgeSony(context);
}
privatestaticvoidsetBadgeSamsung(Context context, int count) {
StringlauncherClassName= getLauncherClassName(context);
if (launcherClassName == null) {
return;
}
Intentintent=newIntent("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);
}
privatestaticvoidsetBadgeSony(Context context, int count) {
StringlauncherClassName= getLauncherClassName(context);
if (launcherClassName == null) {
return;
}
Intentintent=newIntent();
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);
}
privatestaticvoidclearBadgeSony(Context context) {
StringlauncherClassName= getLauncherClassName(context);
if (launcherClassName == null) {
return;
}
Intentintent=newIntent();
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);
}
privatestatic String getLauncherClassName(Context context) {
PackageManagerpm= context.getPackageManager();
Intentintent=newIntent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
StringpkgName= resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
StringclassName= resolveInfo.activityInfo.name;
return className;
}
}
returnnull;
}
}
Solution 2:
In addition to Daniel Ochoa's solution for Samsung (see comment on OP), 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
:Intentintent=newIntent(); 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.
Post a Comment for "Adding Notification Badge On App Icon In Android"