1、小米手机
官方文档:文档中心
/**
* 小米手机创建通知信息并创建角标
*
* @param context
* @param num
*/
public static void setXiaoMiBadgeNum(Context context, int num) {
Log.e(TAG, "--------setXiaoMiBadgeNum----------");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = "消息提示";
String desc = "您有" + num + "条未读消息";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "default";
String channelName = "默认通知";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
Notification notification = new NotificationCompat.Builder(context, "default")
.setContentTitle(title)
.setContentText(desc)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setChannelId("default")
.setNumber(num)//桌面角标数量
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build();
//取消掉上一条通知消息
notificationManager.cancel(notificationId);
try {
Field field = notification.getClass().getDeclaredField("extraNotification");
Object extraNotification = field.get(notification);
Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
method.invoke(extraNotification, num);
} catch (Exception e) {
e.printStackTrace();
}
notificationManager.notify(notificationId, notification);
}
需要注意以下几点:
1)发送通知必须当app处于后台才会生成桌面角标,否则只会生成一次。
2)该方法目前支持到miui12.
2、三星手机
已在三星s7dege+上,Android8.0上通过
public static void setSamSungBadgeNum(Context context,int num){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
mBuilder.setContentTitle("test");
mBuilder.setTicker("test");
mBuilder.setContentText("test");
//点击set 后,app退到桌面等待3s看效果(有的launcher当app在前台设置未读数量无效)
final Notification notification = mBuilder.build();
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 = Math.max(0, Math.min(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("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);
}
protected static void setNotification(Notification notification, int notificationId, Context context) {
if (notification != null) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
}
}
protected static String getLauncherClassName(Context context) {
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
// To limit the components this Intent will resolve to, by setting an
// explicit package name.
intent.setPackage(context.getPackageName());
intent.addCategory(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 = packageManager.resolveActivity(intent, 0);
}
return info.activityInfo.name;
}
3、华为
<!--华为手机更新应用桌面角标需要的权限-->
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE"/>
private void setHUAWEIIconBadgeNum(Context context,int count) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
mBuilder.setContentTitle("test");
mBuilder.setTicker("test");
mBuilder.setContentText("test");
//点击set 后,app退到桌面等待3s看效果(有的launcher当app在前台设置未读数量无效)
final Notification notification = mBuilder.build();
setNotification(notification, notificationId, context);
Bundle bunlde = new Bundle();
bunlde.putString("package", context.getPackageName());
bunlde.putString("class", getLauncherClassName(context));
bunlde.putInt("badgenumber", count);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/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("com.oppo.unsettledevent");
intent.putExtra("pakeageName", context.getPackageName());
intent.putExtra("number", number);
intent.putExtra("upgradeNumber", number);
if (canResolveBroadcast(context, intent)) {
context.sendBroadcast(intent);
} else {
try {
Bundle extras = new Bundle();
extras.putInt("app_badge_count", number);
context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extras);
} catch (Throwable t) {
t.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean canResolveBroadcast(Context context, Intent intent) {
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> receivers = packageManager.queryBroadcastReceivers(intent, 0);
return receivers != null && receivers.size() > 0;
}
5、VIVO
官方文档:vivo开放平台
public static void setBadgeNumber(Context context, int number) {
try {
Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
intent.putExtra("packageName", context.getPackageName());
String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
intent.putExtra("className", launchClassName);
intent.putExtra("notificationNum", number);
context.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
参考文章:
ShortcutBadger
BadgeUtil
android 为桌面图标添加数字角标
最新安卓提醒及角标功能实现概总