前言
最近在开发一款IM类APP,相信大家说起IM第一个就会想到微信。然而相对应的就是要参考微信的效果,来实现我们自己IM产品的角标问题。其实角标最开始应该是在IOS出现的,然后android端也开始了一波跟风操作。也就意味着你要兼容至少国内的几大品牌商(华为、小米、oppo、vivo等)。
开始
从各个厂商的开发者平台上面找解决方案
1、华为
下面的网址为华为角标官方文档,大家可以参考下面说明进行设置。
https://developer.huawei.com/consumer/cn/doc/30801
也贴下部分代码吧
1、声明权限
<uses - permission android: name = "android.permission.INTERNET" / >
<uses - permission android: name = "com.huawei.android.launcher.permission.CHANGE_BADGE " / >
2、在需要进行角标显示地方,采用如下方法传递数据给华为桌面应用。
Bundle extra = new Bundle();
extra.putString("package", "xxxxxx");
extra.putString("class", "yyyyyyy");
extra.putInt("badgenumber", i);
context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
关键参数说明:
package:应用包名
class:桌面图标对应的应用入口Activity类
badgenumber:角标数字
2、小米
官方文档说明
https://dev.mi.com/console/doc/detail?pId=939
它需要传递一个Notification对象.
try {
Field field = notification.getClass().getDeclaredField(“extraNotification”);
Object extraNotification = field.get(notification);
Method method = extraNotification.getClass().getDeclaredMethod(“setMessageCount”, int.class);
method.invoke(extraNotification, mCount);
} catch (Exception e) {
e.printStackTrace();
}
3、OPPO
相比于前面2个,oppo和vivo在其官网上面并没有找到对应的支持文档。只能去各个地方找寻答案了。
try {
if (count == 0) {
count = -1;
}
Intent intent = new Intent("com.oppo.unsettledevent");
intent.putExtra("pakeageName", getReactApplicationContext().getPackageName());
intent.putExtra("number", count);
intent.putExtra("upgradeNumber", count);
if (canResolveBroadcast(getReactApplicationContext(), intent)) {
getReactApplicationContext().sendBroadcast(intent);
} else {
try {
Bundle extras = new Bundle();
extras.putInt("app_badge_count", count);
getReactApplicationContext().getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extras);
} catch (Throwable t) {
t.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
4、VIVO
网上找到的相关代码
Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
intent.putExtra("packageName", getReactApplicationContext().getPackageName());
String launchClassName = getReactApplicationContext().getPackageManager().getLaunchIntentForPackage(getReactApplicationContext().getPackageName()).getComponent().getClassName();
intent.putExtra("className", launchClassName);
intent.putExtra("notificationNum", count);
getReactApplicationContext().sendBroadcast(intent);
测试效果
机型 | 效果 |
---|---|
荣耀6 | 显示 |
vivo x9s | 不显示 |
oppo r9 | 不显示 |
小米8 | 显示 |
总结
- 相对来说小米和华为还是可以兼容的,且官方也提供了相应的文档。
- 咨询oppo的客服人员,需要申请。
- 咨询vivo的客服人员,目前角标的服务还未全面开放。
- 适配太难了…
最后
目前相对来说,使用最多的角标库
https://github.com/leolin310148/ShortcutBadger