1、先大致说下主流厂商目前对角标的支持吧
(以下方案除、索尼、htc 、联想未验证过、其余都有验证。)
华为:支持、
小米:支持、
vivo:中高端支持、老版本不支持(7.0、8.0设备)、
oppo:、需要申请:【应用服务】教你如何给应用软件申请Push角标功能 - 应用 - 开发者社区
三星:支持、
2、小米、vivo oppo 适配注意的点
先说小米:小米角标和通知相关、即通知数量==角标显示数量、官方似乎也意识到了此设计的缺陷、提出了解决方案:文档中心。
通知消失/app重启 角标都会消失。
oppo 申请完成后、默认开启桌面数字角标;
oppo(老设备):同小米
小米 vivo oppo(老设备):需要在通知权限中、开启圆角通知(不同设备叫法不一样、注意区分)。
3、相关代码
3-1、角标设置类(核心)
public class BadgerUtils {
public static int notificationId = 100;
//xiaomi oppo 通用
public static void setNotificationBadge(Context context, int count) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService
(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 8.0之后添加角标需要NotificationChannel
NotificationChannel channel = new NotificationChannel("badge", "齐齐乐通知角标",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(true);
channel.enableLights(false);
channel.enableVibration(false);
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
}
if (!(context instanceof Activity)) {
Log.d("BadgerUtils", "xiaomi or oppo not set On MainActivity、it's wrong");
return;
}
String content=count==0?"暂无通知":"您有" + count + "条未读消息";
Activity activity = (Activity) context;
Intent intent = new Intent(context, activity.getClass());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context, "badge")
.setContentTitle("齐齐乐")
.setContentText(content)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap
.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setChannelId("badge")
.setNumber(count)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).build();
// 小米
try {
Field field = notification.getClass().getDeclaredField("extraNotification");
Object extraNotification = field.get(notification);
Method method = extraNotification.getClass().getDeclaredMethod("extraNotification", int.class);
method.invoke(extraNotification, count);
} catch (Exception e) {
e.printStackTrace();
}
notificationManager.notify(notificationId++, notification);
}
//华为
public static boolean setHuaweiBadge(Context context, int count) {
try {
String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
Bundle bundle = new Bundle();
bundle.putString("package", context.getPackageName());
bundle.putString("class", launchClassName);
bundle.putInt("badgenumber", count);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher" +
".settings/badge/"), "change_badge", null, bundle);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
//vivo
public static void setVivoBadge(Context context, int count) {
try {
String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
Intent intent = new Intent();
intent.setAction("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
intent.putExtra("packageName", context.getPackageName());
intent.putExtra("className", launchClassName);
intent.putExtra("notificationNum", count);
/* if (Build.VERSION.SDK_INT>=26) {
intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
}*/
context.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
//三星
public static void setSamsungBadge(Context context, int count) {
try {
String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
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", launchClassName);
context.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
// 联想ZUK(支持)
public static void setZukBadge(Context context, int count) {
try {
Bundle extra = new Bundle();
ArrayList<String> ids = new ArrayList<>();
// 以列表形式传递快捷方式id,可以添加多个快捷方式id
// ids.add("custom_id_1");
// ids.add("custom_id_2");
extra.putStringArrayList("app_shortcut_custom_id", ids);
extra.putInt("app_badge_count", count);
Uri contentUri = Uri.parse("content://com.android.badge/badge");
Bundle bundle = context.getContentResolver().call(contentUri, "setAppBadgeCount", null,
extra);
} catch (Exception e) {
e.printStackTrace(