1、小米手机
官方文档:文档中心
/**
* 小米手机创建通知信息并创建角标
*
* @param context
* @param num
*/
public static void setXiaoMiBadgeNum(Context context, int num) {
(TAG, "--------setXiaoMiBadgeNum----------");
NotificationManager notificationManager = (NotificationManager) (Context.NOTIFICATION_SERVICE);
String title = "消息提示";
String desc = "您有" + num + "条未读消息";
if (.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "default";
String channelName = "默认通知";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
(true);
(channel);
}
Notification notification = new (context, "default")
.setContentTitle(title)
.setContentText(desc)
.setWhen(())
.setSmallIcon(.ic_launcher)
.setAutoCancel(true)
.setChannelId("default")
.setNumber(num)//桌面角标数量
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build();
//取消掉上一条通知消息
(notificationId);
try {
Field field = ().getDeclaredField("extraNotification");
Object extraNotification = (notification);
Method method = ().getDeclaredMethod("setMessageCount", );
(extraNotification, num);
} catch (Exception e) {
();
}
(notificationId, notification);
}
需要注意以下几点:
1)发送通知必须当app处于后台才会生成桌面角标,否则只会生成一次。
2)该方法目前支持到miui12.
2、三星手机
已在三星s7dege+上,Android8.0上通过
public static void setSamSungBadgeNum(Context context,int num){
mBuilder = new (context)
.setSmallIcon(.ic_launcher)
.setWhen(())
.setAutoCancel(true);
("test");
("test");
("test");
//点击set 后,app退到桌面等待3s看效果(有的launcher当app在前台设置未读数量无效)
final Notification notification = ();
sendBadgeNotification(notification, notificationId, context, num, num);
}
/**
* 重置、清除Badge未读显示数<br/>
*
* @param context
*/
public static void resetBadgeCount(Context context) {
sendBadgeNotification(null, 0, context, 0, 0);
}
public static void sendBadgeNotification(Notification notification, int notifyID, Context context, int thisNotifyCount, int count) {
if (count <= 0) {
count = 0;
} else {
count = (0, (count, 99));
}
executeBadge(context, notification, notifyID, thisNotifyCount, count);
}
public static void executeBadge(Context context, Notification notification, int notificationId, int thisNotificationCount, int count) {
setNotification(notification, notificationId, context);
String launcherClassName = getLauncherClassName(context);
if (launcherClassName == null) {
return;
}
Intent intent = new Intent(".BADGE_COUNT_UPDATE");
("badge_count", count);
("badge_count_package_name", ());
("badge_count_class_name", launcherClassName);
(intent);
}
protected static void setNotification(Notification notification, int notificationId, Context context) {
if (notification != null) {
NotificationManager notificationManager = (NotificationManager) (Context.NOTIFICATION_SERVICE);
(notificationId, notification);
}
}
protected static String getLauncherClassName(Context context) {
PackageManager packageManager = ();
Intent intent = new Intent(Intent.ACTION_MAIN);
// To limit the components this Intent will resolve to, by setting an
// explicit package name.
(());
(Intent.CATEGORY_LAUNCHER);
// All Application must have 1 Activity at least.
// Launcher activity must be found!
ResolveInfo info = packageManager
.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
// get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
// if there is no Activity which has filtered by CATEGORY_DEFAULT
if (info == null) {
info = (intent, 0);
}
return ;
}
3、华为
<!--华为手机更新应用桌面角标需要的权限-->
<uses-permission android:name=".CHANGE_BADGE"/>
private void setHUAWEIIconBadgeNum(Context context,int count) {
mBuilder = new (context)
.setSmallIcon(.ic_launcher)
.setWhen(())
.setAutoCancel(true);
("test");
("test");
("test");
//点击set 后,app退到桌面等待3s看效果(有的launcher当app在前台设置未读数量无效)
final Notification notification = ();
setNotification(notification, notificationId, context);
Bundle bunlde = new Bundle();
("package", ());
("class", getLauncherClassName(context));
("badgenumber", count);
().call(("content:///badge/"), "change_badge", null, bunlde);
}
4、OPPO
首先,官方客服和我说他们可以支持,但需要申请。
而抱歉的是截止目前我还没有得到具体的方法,因为我们还在它的申请流程中。。
具体申请流程请去OPPO开放平台找他们的人工客服获取申请方法。
public static void setBadgeNumber(Context context, int number) {
try {
if (number == 0) {
number = -1;
}
Intent intent = new Intent("");
("pakeageName", ());
("number", number);
("upgradeNumber", number);
if (canResolveBroadcast(context, intent)) {
(intent);
} else {
try {
Bundle extras = new Bundle();
("app_badge_count", number);
().call(("content:///badge"), "setAppBadgeCount", null, extras);
} catch (Throwable t) {
();
}
}
} catch (Exception e) {
();
}
}
public static boolean canResolveBroadcast(Context context, Intent intent) {
PackageManager packageManager = ();
List<ResolveInfo> receivers = (intent, 0);
return receivers != null && () > 0;
}
5、VIVO
官方文档:vivo开放平台
public static void setBadgeNumber(Context context, int number) {
try {
Intent intent = new Intent(".CHANGE_APPLICATION_NOTIFICATION_NUM");
("packageName", ());
String launchClassName = ().getLaunchIntentForPackage(()).getComponent().getClassName();
("className", launchClassName);
("notificationNum", number);
(intent);
} catch (Exception e) {
();
}
}
参考文章:
ShortcutBadger
BadgeUtil
android 为桌面图标添加数字角标
最新安卓提醒及角标功能实现概总