根据https://developer.android.com/training/notify-user/badges 从 Android 8 开始,操作系统允许设置应用程序图标通知徽章,但我无法让它工作。
NotificationManagerCompat nm = NotificationManagerCompat.from(activityContext);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel nc = new NotificationChannel("ab", "chanelName", NotificationManager.IMPORTANCE_DEFAULT);
nc.setDescription("A chanel description here");
nc.setShowBadge(true);
NotificationManager nmm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nmm.createNotificationChannel(nc);
}
NotificationCompat.Builder nb = new NotificationCompat.Builder(App.instance, "ab");
nb.setSmallIcon(R.drawable.accept_icon);
nb.setTicker("here is the ticker");
nb.setContentText("Here content text");
nb.setContentTitle("Here content title");
nb.setNumber(5);
nb.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
nm.notify(1, nb.build());
我使用的是 Android 8.1 的 Nexus 6P。 它在系统栏中显示通知,但不显示应用程序图标上的徽章。 我错过了什么?
最佳答案
我认为您的代码很好,但 Nexus 6P 不支持默认启动器上的通知徽章。您需要在其他设备(Pixel 或类似设备 - 当然您可以使用带有 Oreo 或更高版本的模拟器)上进行测试,或者下载支持 Oreo 通知徽章的启动器。
有人也遇到过类似的问题。 在这里阅读更多信息:https://productforums.google.com/forum/#!topic/nexus/NykQ-lp1n2c
关于java - 通过标准 SDK 在 Android 8 上显示应用程序图标徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526171/