简体   繁体   English

如何在Android中为所有设备实现推送通知徽章

[英]How to achieve push notification badge in android for all devices

I want to display push notification badge in all devices like Samsung,Sony,Lg etc., I want to display notification badge like facebook native application. 我想在三星,索尼,Lg等所有设备上显示推送通知徽章,我想显示像facebook原生应用程序的通知徽章。 i did small research for that in my result Android relies on the Notification Center to give the user indication about incoming events. 我做了一个小的研究,在我的结果中,Android依靠通知中心向用户提供有关传入事件的指示。 So technically, if your app already does that, you don't have to do anything else. 从技术上讲,如果您的应用已经这样做,您不必做任何其他事情。 but some devices have own framework badge receivers. 但有些设备有自己的框架徽章接收器。

In sony 在索尼

http://marcusforsberg.net/blog/android-notification-badge-app-icon-sony/ http://marcusforsberg.net/blog/android-notification-badge-app-icon-sony/

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

In Samsung 在三星

https://github.com/shafty023/SamsungBadger https://github.com/shafty023/SamsungBadger

Add the following permissions to your application's AndroidManifest.xml 将以下权限添加到应用程序的AndroidManifest.xml中

uses-permission android:name="com.sec.android.provider.badge.permission.READ" 
uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" 

To badge your icon with "1" 用“1”标记图标

Context context = getApplicationContext();
if (Badge.isBadgingSupported(context)) {
    Badge badge = new Badge();
    badge.mPackage = context.getPackageName();
    badge.mClass = getClass().getName();
    badge.mBadgeCount = 1;
    badge.save(context);
}

Is there any other framework badge receivers code is available? 是否有其他框架徽章接收器代码可用?

I also faced same problem but perfect solution for your question is below link: 我也面临同样的问题,但你的问题的完美解决方案是以下链接:

https://derivedcode.wordpress.com/2013/10/09/showing-badge-or-count-in-android-app-icon/ https://derivedcode.wordpress.com/2013/10/09/showing-badge-or-count-in-android-app-icon/

Try to use this libriary ShortcutBadger . 尝试使用此libriary ShortcutBadger

Description from github: github的描述:

ShortcutBadger: The ShortcutBadger makes your Android App showing the count of unread messages as a badge on your App shortcut! ShortcutBadger:ShortcutBadger使您的Android应用程序在您的应用程序快捷方式上显示未读邮件的数量作为徽章!

This works for me: 这对我有用:

1) Add mavenCentral to your build script.

 repositories {
    mavenCentral()
}

2) Add dependencies for ShortcutBadger, it's available from maven now.

 dependencies {
        compile 'me.leolin:ShortcutBadger:1.1.4@aar'
    }

3) Add the codes below:

int badgeCount = 1;
ShortcutBadger.applyCount(context, badgeCount); //for 1.1.4
ShortcutBadger.with(getApplicationContext()).count(badgeCount); //for 1.1.3

4) If you want to remove the badge
 ShortcutBadger.removeCount(context); //for 1.1.4
ShortcutBadger.with(getApplicationContext()).remove();  //for 1.1.3

 or

ShortcutBadger.applyCount(context, 0); //for 1.1.4
ShortcutBadger.with(getApplicationContext()).count(0); //for 1.1.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM