在 Android O 开发者预览版中,Google 引入了通知徽章,应该显示在启动器图标上。我正在使用从 dev 渠道获得的 Android O 模拟器。我编写了一个简单的代码来显示通知徽章,但似乎无法工作 -
Notification notification = new Notification.Builder(getApplicationContext())
.chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
.setSmallIcon(android.R.drawable.btn_star)
.setNumber(10)
.build();
mNotificationManager.notify(1, notification);
它只会显示普通的通知。
API - https://developer.android.com/reference/android/app/Notification.Builder.html#chooseBadgeIcon(int)。
有人已经在处理这个问题了吗?我是不是漏掉了什么?
在设置中启用了徽章显示。
我也尝试了使用NotificationChannel,但不起作用 -
NotificationChannel mChannel = new NotificationChannel("TestBadge_id", "TestBadgeName", NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setShowBadge(true);
mNotificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(getApplicationContext())
.chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
.setSmallIcon(android.R.drawable.btn_star)
.setNumber(10)
.setChannel("TestBadge_id")
.build();
mNotificationManager.notify(1, notification);
NotificationChannel
上使用setShowBadge(true)
,然后在创建Notification
时使用该通道。 - CommonsWare