57

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

Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89
epeleg
  • 9,293
  • 16
  • 91
  • 144
  • Sure that isn't a widget, rather than just an app icon? – Gabe Sechan Jul 07 '13 at 08:31
  • When other questions do not have an answer, you should not start a new one. – Waza_Be Jul 07 '13 at 08:33
  • @GabeSechan It is not listed in the available widgets list as far as I could tell. Is it possible to have widget that is not listed? – epeleg Jul 07 '13 at 10:02
  • 2
    @Waza_Be all those Q's seem irrelevant. I am not asking how can this be done or if it can be done. I want to know how was it done. not only that but most of the other Q's ARE closed with an answer saying that it can not be done. (maybe they need to be updated). Any way again - I want to know what is it that I see in the new FB app and how does it work... – epeleg Jul 07 '13 at 10:05
  • 4
    @bill-the-lizard - I am not sure I understand why the off topic nor why did I get the down marks... Many people want to be able to achieve this goal of displaying numeric badges over the app icons of android applications. The fact that such a behavior is shown by some app (FB) gave me reason to believe that maybe someone can explain how they did it and maybe allow me to achieve a similar effect on my app. I will edit to clarify some of this in the text of the Q. – epeleg Jul 07 '13 at 19:55
  • 3
    @bill-the-lizard closing this as off-topic was a bad call. TouchWiz is clearly a relevant part of Android despite the fact it is not "pure" Android. Yes this feature is limited to select developers, but it's clearly not off-topic. – radley Jul 26 '13 at 18:59
  • @radley None of what you're saying is relevant to the specific reason this was closed. – Bill the Lizard Jul 26 '13 at 19:14
  • I found this question useful as i was searching for exactly this thing. Also its not samsung / touchWiz specific.. i can see it on my sony phone! – Umair Jul 31 '13 at 08:01
  • 1
    Thanks you both radley & @Umair. Since writing this Q I have play quite a lot with push messages via my phonegap app. I agree with bill-the-lizard that my knowledge about the topic is probably not sufficient, but I am NOT an android developer, I use phone gap build and supported plugins. still my low knowledge of android does not seem to me like a good reason to close the Q. I would STILL like a better explanation of what is going on here (i.e. a technical explanation of how FB do what they do) and even more so following Umair's added info that it works on his Sony phone. – epeleg Jul 31 '13 at 08:14
  • 4
    This is available on stock android. I don't know how. I don't know why .. but I am looking at a badge on my facebook app icon right now, on stock android, running on the Xperia Z, this question should be un-closed – Steven Elliott Aug 13 '13 at 16:07
  • 3
    I have written up a how-to for doing this here http://stackoverflow.com/questions/20136483/how-do-you-interface-with-badgeprovider-on-samsung-phones/20136484#20136484 – Daniel Ochoa Nov 22 '13 at 03:41

5 Answers5

31

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);
Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89
  • 2
    Here, how to get badgeCount when app in background and push notification came? – Avinash May 24 '17 at 08:51
  • Hi, Thanks for the library. However I am getting build errors for release build. The error is [Duplicate zip entry](https://stackoverflow.com/q/30418150/1911652). I tried all possible solutions they given on SO however still couldn't get through. Please can you throw light. – Atul Mar 14 '18 at 11:22
  • I have integrated this lib, but it's not working with samsung s5 and , samsung s7, if application is in dead state(closed and removed from task manager)... :( – zephyr May 10 '18 at 12:05
  • This may be a stupid question but how do you install it using npm? – Sarotobi Sep 01 '18 at 01:17
23

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.

Community
  • 1
  • 1
Marcus
  • 1,623
  • 1
  • 16
  • 22
12

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.

Mogsdad
  • 40,814
  • 19
  • 140
  • 246
Jorge Arimany
  • 5,013
  • 2
  • 26
  • 21
10

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.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • fair enough - so its limited to Samsung devices... still I hope maybe someone has more info on how this is done even within this "limited" context... – epeleg Jul 07 '13 at 19:48
  • He didn't ask about "stock" Android in his question, so why is that relevant? Are you suggesting TouchWiz development has no place here? – radley Jul 26 '13 at 19:02
  • 1
    @radley: It has no place here, as Samsung engineers have better ways of getting this information, and only Samsung engineers work on Samsung's TouchWiz home screen. Of course, it is conceivable that Samsung has published some SDK that allows for badging of launcher icons, as I mentioned in my answer. – CommonsWare Jul 26 '13 at 19:06
  • 2
    I am using stock android on the Sony Xperia Z ... and my facebook app icon has these badges, so all the "it's touch wiz" comments are wrong. This is the normal app icon, not a widget. This question should be un-closed. – Steven Elliott Aug 13 '13 at 16:06
  • 3
    @StevenElliott: I saw another question and answer elsewhere on this topic recently, suggesting that what they are doing is using a series of `` elements, each with different icons, and so the app has, say, 100 icons baked in (e.g., one unbadged, 99 with badges from 1 through 99), and they are enabling and disabling components to switch between them. If so, that's going to be terribly unreliable, as not all home screens will necessarily update their icons, let alone references elsewhere (e.g., shortcuts). Regardless, there's nothing in the Android SDK directly for this feature. – CommonsWare Aug 13 '13 at 16:12
  • 1
    Can confirm the `` route works fine on HTC Desire 2.2, but not reliably on Galaxy Nexus 4.2.2 or Nexus 7 4.3. I have found no way to reliably hack around this issue on the newer devices - my conclusion is "don't try it". – Richard Le Mesurier Nov 17 '13 at 09:44
0

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
ZEHINZ
  • 231
  • 3
  • 9