我们广大用户对于通知消息栏和桌面角标都很熟悉,Google Android 是在 8.0 以后加入的,但是对于部分国内厂商较早就有尝试,和尚今天对桌面角标进行简单尝试;
对于桌面角标的兼容处理,华为是最明确最容易处理的,官方文档 清晰明了,和尚按照官方介绍尝试如下:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
public static void setHuaweiBadge(Context context, int count) {
try {
Bundle badgeBundle = new Bundle();
badgeBundle.putString("package", "包名");
badgeBundle.putString("class", "启动页");
badgeBundle.putInt("badgenumber", count);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, badgeBundle);
} catch (Exception e) {
e.printStackTrace();
}
}
setHuaweiBadge(getApplicationContext(), 10);
小米官网 提供了明确的角标处理方式,但小米的处理方式必须依赖通知栏消息才可以对桌面角标进行处理;
private void notifyChannel() {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
android.content.Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel =
new NotificationChannel(Constants.PUSH_CHANNEL_ID, "mi_push",
NotificationManager.IMPORTANCE_HIGH);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
Notification notification = new NotificationCompat.Builder(this, "mi_push")
.setContentTitle("您收到一条消息")
.setContentText("设置桌面角标")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setAutoCancel(true)
.build();
getMiBadge(notification, 10);
notificationManager.notify(1, notification);
}
public static void getMiBadge(Notification notification, int count) {
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();
}
}
三星和 LG 厂商的集成方式一致;都是通过广播发送通知,然后再展示桌面角标;
<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
private static void setSumsungBadge(Context context, int count) {
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", "包名");
intent.putExtra("badge_count_class_name", "启动页");
context.sendBroadcast(intent);
}
setSumsungBadge(getApplicationContext(), 10);
联想 ZUK 厂商在开发者平台上提供了具体的角标展示方案,其中可设置 ids 对多图标设置角标,和尚仅尝试了主 app 图标角标展示;
<uses-permission android:name="android.permission.READ_APP_BADGE"/>
private static void setZUKBadge(Context context, int count) {
Bundle extra = new Bundle();
extra.putInt("app_badge_count", count);
context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extra);
}
setZUKBadge(getApplicationContext(), 10);
OPPO 开发者平台未提供处理桌面角标的方法,联系客服需要提供相关材料申请角标权限,原则上只通过应用的 IM 类提醒功能角标申请;申请之后 OPPO 相关工作人员会单独建群指导说明,和尚测试应用未通过要求,暂不支持;
查阅 VIVO / 魅族 开发者平台,明确指出暂不支持桌面角标的展示,故和尚暂未尝试;
为了良好的用户体验,桌面角标的展示应慎重合理,这也是部分尝试暂不提供桌面角标展示的原因;而且在国内的应用大部分需要单独集成各厂商的 SDK,和尚研究不深,有错误的话请多多指导!