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

假如想象 提交于 2019-11-26 06:00:14

问题


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


回答1:


Hi you can use this lib simply.

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

ShortcutBadger

Add :

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

Remove :

ShortcutBadger.applyCount(context, 0);



回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/17510419/how-does-facebook-add-badge-numbers-on-app-icon-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!