我正在使用装有Android Oreo SDK的Google Nexus 5x进行测试。在主屏幕上,我无法在应用程序图标中找到通知徽章,即使我收到了来自应用程序的通知。此外,应用程序快捷方式也没有显示数字。以下是代码片段:
final NotificationManager mNotific=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence name="Ragav";
String desc="this is notific";
int imp=NotificationManager.IMPORTANCE_HIGH;
final String ChannelID="my_channel_01";
NotificationChannel mChannel=new NotificationChannel(ChannelID,name,imp);
mChannel.setDescription(desc);
mChannel.setLightColor(Color.CYAN);
mChannel.canShowBadge();
mChannel.setShowBadge(true);
mNotific.createNotificationChannel(mChannel);
final int ncode=1;
String Body="This is testing notific";
final Notification n= new Notification.Builder(getApplicationContext(),ChannelID)
.setContentTitle(getPackageName())
.setContentText(Body)
.setNumber(5)
.setBadgeIconType(R.mipmap.ic_launcher_round)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true).build();
for(int i=0;i<25;i++) {
Thread.sleep(1000);
mNotific.notify(ncode, n);
}
.setNumber(5)
设置的数字显示在通知徽章上。我已经复制粘贴了您的代码,没有使用for
循环(只调用一次mNotific.notify(ncode, n)
),它可以100%地工作,会显示通知徽章。确保您正在测试的设备运行Android Oreo,清除您的应用程序数据(以删除所有先前创建的通知通道),删除for
循环和Thread.sleep(1000);
,然后再次测试,它应该可以工作。 - Wess