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

4

34

With Sony's Xperia Home, certain apps have the ability to display a count bubble or badge on the app icon. Facebook and Facebook Messenger both do this, as well as the built in Email app.

This has been solved for Samsung's launcher, but I have not come across any documentation on how to do it for Sony's launcher.

How can it be done?

Engrain answered 26/11, 2013 at 12:13 Comment(3)
I can't post this as an answer since I don't have enough reputation to answer my own question within 8 hours, but here is a guide on how to do it: marcusforsberg.net/blog/…. I will post this as an answer and accept it later today, when I'm allowed to.Engrain
I have found this answer for viewBadge. github.com/jgilfelt/android-viewbadger Just go through it and explore it works properly in your case. let me know if anyone facing issues for it.Diandiana
Possible duplicate of Is there a way to add a badge to an application icon in Android?Centroid
E
49

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! :)

Engrain answered 26/11, 2013 at 15:30 Comment(4)
Since this is a broadcast receiver I'm guessing there's no way to query your current badge record? Also, does Sony differentiate between UPDATE_BADGE AND CREATE_BADGE? Do you always send UPDATE_BADGE even if you've never created a badge record before?Tice
@Daniel Ochoa From what I gather, UPDATE_BADGE is always used, no CREATE_BADGE. And yes, UPDATE_BADGE will be used even if there's no previous badge. And no, I don't think you can see what the badge currently says - you'd have to keep track of that manually. :)Engrain
As an update on this, the third-party launcher Apex also supports badges in a similar fashion to Sony's launcher: forum.xda-developers.com/showthread.php?t=1930365Engrain
@Engrain Can this code only for Sony Xperia devices?Cistern
S
30

I use this class for Samsung, Sony and HTC 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;
    }
}
Stott answered 15/9, 2014 at 13:27 Comment(2)
Hi does anybody know what the min sdk requirement is for the badge update in samsung and sony devices? Thanks in advance.Terminate
Awesome work! Hope support more phone such as xiaomi, acer, htc and MotorolaMita
M
0

Well, this is pretty difficult to do. The only way I've found so far is to create a widget that will handle both the app icon and the badge.

I highly suggest you to visit this page where you'll learn how to achieve that: Android: Is it possible to update a ImageView/ImageButton with a number to show the number of new messages?

Morrill answered 26/11, 2013 at 12:16 Comment(1)
Heh, I have actually figured it out and wanted to post an answer to my own question but I can't do that without 10 reputation... Oops. :P Will post a link to the answer as a comment in a bit and post it as an answer in 8 hours, when I'm allowed to.Engrain
T
0

I realize that this question is quite old, but for historical purposes, the API for 3rd party applications to interact with the Xperia Home API for this particular feature was made public last year:

Xperia Home badge API now publicly available

With sample code here:

sonyxperiadev/home-badge

There is also a 3rd party library which supports most major phone vendors, including the Xperia Home API:

leolin310148/ShortcutBadger

Tb answered 20/7, 2017 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.