通知徽章未在Android O中唤醒 [英] Notification Badges not woking in Android O
问题描述
在Android O开发人员预览版中,Google引入了通知徽章,这些徽章应该显示在启动器图标上.我在开发人员频道中将模拟器与Android O配合使用.我编写了一个简单的代码来显示通知徽章,但似乎不起作用-
通知通知=新的Notification.Builder(getApplicationContext()).chooseBadgeIcon(Notification.BADGE_ICON_SMALL).setSmallIcon(android.R.drawable.btn_star).setNumber(10).建造();mNotificationManager.notify(1,通知);
它只是显示为正常通知.
API-
也尝试过NotificationChannel.不起作用-
NotificationChannel mChannel =新的NotificationChannel("TestBadge_id","TestBadgeName",NotificationManager.IMPORTANCE_HIGH);mChannel.enableLights(true);mChannel.setLightColor(Color.RED);mChannel.setShowBadge(true);mNotificationManager.createNotificationChannel(mChannel);通知通知=新Notification.Builder(getApplicationContext()).chooseBadgeIcon(Notification.BADGE_ICON_SMALL).setSmallIcon(android.R.drawable.btn_star).setNumber(10).setChannel("TestBadge_id").建造();mNotificationManager.notify(1,通知);
在早期的预览版本中, Android-O
的Notification徽章示例在模拟器中似乎不起作用.但是,对于最新发布的Android-O开发人员 preview-3
,徽章会按照
长按时,如果有多个通知,则显示计数.该计数将根据活动的通知自动递增/递减.您还可以使用
确保您定位到最新的API:
compileSdkVersion 26buildToolsVersion"26.0.0"targetSdkVersion 26
在像素Android模拟器版本 26.1.1
中进行了测试.
With Android O developer preview google has introduced notification badges that are supposed to be shown on launcher icon. I am using emulator with Android O from dev channel.I wrote a simple code to show notification badge but it does not seem to work -
Notification notification = new Notification.Builder(getApplicationContext())
.chooseBadgeIcon(Notification.BADGE_ICON_SMALL)
.setSmallIcon(android.R.drawable.btn_star)
.setNumber(10)
.build();
mNotificationManager.notify(1, notification);
It just shows as normal notification.
API - https://developer.android.com/reference/android/app/Notification.Builder.html#chooseBadgeIcon(int)
Has anyone worked on this yet? Am I missing something?
Show badge is enabled in settings.
Tried with NotificationChannel too. Does not work -
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);
The Notification badge examples of Android-O
doesn't seem to work in emulator in early preview releases. But with the latest release of Android-O developer preview-3
the badges are displayed properly as documented in the Notification Badges section.
To display notification badge, you need to set the setShowBadge(boolean) for a notification channel to true
. By default badges will be displayed like below:
Upon long press if there is a more than a one notification the count is displayed.The count automatically increments/decrements based on the active notifications. You can also adjust the count manually by using the Notification.Builder.setNumber().
Sample showing notification count on long press of launcher icon:
Make sure you are targetting the latest API:
compileSdkVersion 26
buildToolsVersion "26.0.0"
targetSdkVersion 26
Tested in pixel Android emulator version 26.1.1
.
这篇关于通知徽章未在Android O中唤醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!