前言
应用的角标提醒,这个功能初始是苹果自带的,后来其他Android手机厂商看到了这个角标的优点,纷纷在自己的操作系统中做了各自的角标功能,所以如果我们需要修改角标,需要根据厂商的要求进行适配。
部分机型完成适配,代码可能在某些机型上不起作用。
区分厂商
Android中使用android.os.Build.MANUFACTURER
方法来获取当前设备的厂商,并且使用equalsIgnoreCase("xiaomi")
方法来判断是否是改厂商的设备。
设置角标
可以前往对应厂商的开发者平台查看对应的文档,里面都有,只不过有些写的很粗略。
小米
小米手机无法单独设置角标,需要在现实通知的时候携带角标数量
/**
* count 角标数量
*/
private static void setBadgeOfMIUI(Context context, int count) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService
(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
Log.e("BadgeUtil", "小米设备角标设置失败!");
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 8.0之后添加角标需要NotificationChannel
NotificationChannel channel = new NotificationChannel("badge", "badge",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
// 点击消息通知后跳转的页面,可以携带参数
Intent intent = new Intent(context, WebActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
Notification notification = new NotificationCompat.Builder(context, "badge")
.setContentTitle("消息通知标题")
.setContentText("消息通知内容")
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ihive02))
.setSmallIcon(R.drawable.ihive02)
.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("setMessageCount", int.class);
method.invoke(extraNotification, count);
} catch (Exception e) {
e.printStackTrace();
}
notificationManager.notify((int) (System.currentTimeMillis() / 1000), notification);
}
vivo
需要申请权限,并且改权限会弹出对应的权限申请弹窗,同意即可
<!--vivo角标-->
<uses-permission android:name="com.vivo.notification.permission.BADGE_ICON" />
然后代码中设置角标
private static void setBadgeOfVIVO(Context context, int count) {
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", count);
context.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
注:vivo手机如果需要开启角标显示的话,还需要用户手动开启对应权限,需要在代码中进行跳转到手动开启权限的页面。
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);
华为和荣耀
华为和荣耀的一起写,是因为我这边测试出来使用同一个代码分别在华为和荣耀手机上面都可以生效
权限的申请
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
<uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />
代码中设置角标
private static void setBadgeOfHuaWei(Context context, int count) {
//权限检查
try {
Bundle bundle = new Bundle();
bundle.putString("package", context.getPackageName());
bundle.putString("class", getLauncherClassName(context));
bundle.putInt("badgenumber", count);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getLauncherClassName(Context context) {
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setPackage(context.getPackageName());
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (info == null) {
info = packageManager.resolveActivity(intent, 0);
}
return info.activityInfo.name;
}
OPPO
OPPO手机无法申请角标应用,也就是无法设置角标。
官方客服人员回应:由于平台运营策略调整暂停数字角标申请,具体恢复时间待定。