修改通知标记
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
从 Android 8.0(API 级别 26)开始,当关联的应用有活动通知时,启动器图标上会显示通知标志(也称为通知圆点)。用户可以轻触并按住应用图标以显示通知以及任何应用快捷方式,如图 1 所示。
默认情况下,这些圆点会显示在支持圆点的启动器应用中,您的应用无需执行任何操作。然而,在某些情况下,您可能不希望显示通知圆点,或者想要精确控制要在其中显示哪些通知。
图 1. 通知标志和轻触并按住菜单。
停用标志
在某些情况下,您的通知并不适合显示标志,您可以通过对 NotificationChannel
对象调用 setShowBadge(false)
,针对每个渠道停用标志。
例如,您可能希望在以下情况下停用通知标志:
- 持续显示的通知:大多数持续显示的通知(例如图片处理、媒体播放控件或当前导航说明)都不适合显示为标志。
- 日历提醒:避免当前时间发生标志事件。
- 时钟或闹钟事件:避免为与当前闹钟相关的通知设置标志。
以下示例代码演示了如何针对某个通知渠道隐藏标志:
Kotlin
val id = "my_channel_01"
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(id, name, importance).apply {
description = descriptionText
setShowBadge(false)
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
Java
String id = "my_channel_01";
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.setShowBadge(false);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(mChannel);
设置自定义通知计数
默认情况下,轻触并按住菜单上显示的数字会随着通知数量增加(如图 1 所示),但您可以针对自己的应用替换此数字。例如,如果您仅使用一个通知来表示多条新信息,但您希望此处的计数代表新信息总数,这样做可能会很有用。
如需设置自定义数字,请对通知调用 setNumber()
,如下所示:
Kotlin
var notification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setNumber(messageCount)
.build()
Java
Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setNumber(messageCount)
.build();
修改通知的轻触并按住菜单图标
轻触并按住菜单会显示与通知关联的大图标或小图标(如果有)。默认情况下,系统会显示大图标,但是您可以调用 Notification.Builder.setBadgeIconType()
并传入 BADGE_ICON_SMALL
常量以显示小图标。
Kotlin
var notification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build()
Java
Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("New Messages")
.setContentText("You've received 3 new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.build();
隐藏重复的快捷方式
如果您的应用创建了一条与应用快捷方式重复的通知,您可以调用 setShortcutId()
,在通知处于活动状态时暂时隐藏该快捷方式。
如需获取更多使用通知的示例代码,请参阅 SociaLite 示例应用。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Modify a notification badge\n\nStarting with Android 8.0 (API level 26), notification badges---also known as\nnotification dots---appear on a launcher icon when the associated app has an\nactive notification. Users can\ntouch \\& hold the app icon to reveal the notifications, along with any\n[app shortcuts](/guide/topics/ui/shortcuts), as shown in\nfigure 1.\n\nThese dots appear by default in launcher apps that support them, and there's\nnothing your app needs to do. However, there might be situations in which you\ndon't want the to notification dot to appear or you want to control exactly\nwhich notifications appear there.\n\n\n**Figure 1.** Notification badges and the touch \\& hold menu.\n\n\u003cbr /\u003e\n\nDisable badging\n---------------\n\nThere are cases where badges don't make sense for your notifications, so you\ncan disable them on a per-channel basis by calling\n[`setShowBadge(false)`](/reference/android/app/NotificationChannel#setShowBadge(boolean))\non your [`NotificationChannel`](/reference/android/app/NotificationChannel)\nobject.\n\nFor example, you might want to disable notification badges in the following\nsituations:\n\n- Ongoing notifications: most ongoing notifications, such as image processing, media playback controls, or current navigation instructions, don't make sense as a badge.\n- Calendar reminders: avoid badging events occurring at the current time.\n- Clock or alarm events: avoid badging notifications related to current alarms.\n\nThe following sample code demonstrates how to hide badges for\na notification channel: \n\n### Kotlin\n\n```kotlin\nval id = \"my_channel_01\"\nval name = getString(R.string.channel_name)\nval descriptionText = getString(R.string.channel_description)\nval importance = NotificationManager.IMPORTANCE_LOW\nval mChannel = NotificationChannel(id, name, importance).apply {\n description = descriptionText\n setShowBadge(false)\n}\nval notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager\nnotificationManager.createNotificationChannel(mChannel)\n```\n\n### Java\n\n```java\nString id = \"my_channel_01\";\nCharSequence name = getString(R.string.channel_name);\nString description = getString(R.string.channel_description);\nint importance = NotificationManager.IMPORTANCE_LOW;\nNotificationChannel mChannel = new NotificationChannel(id, name, importance);\nmChannel.setDescription(description);\nmChannel.setShowBadge(false);\n\nNotificationManager notificationManager =\n (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);\nnotificationManager.createNotificationChannel(mChannel);\n```\n\nSet custom notification count\n-----------------------------\n\nBy default, each notification increments a number displayed on the touch \\& hold\nmenu, as shown in figure 1, but you can override this number for your app.\nFor example, this might be useful if you're using just one notification to\nrepresent multiple new messages but want the count to represent the\nnumber of total new messages.\n\nTo set a custom number, call\n[`setNumber()`](/reference/androidx/core/app/NotificationCompat.Builder#setNumber(int))\non the notification, as shown here: \n\n### Kotlin\n\n```kotlin\nvar notification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)\n .setContentTitle(\"New Messages\")\n .setContentText(\"You've received 3 new messages.\")\n .setSmallIcon(R.drawable.ic_notify_status)\n .setNumber(messageCount)\n .build()\n```\n\n### Java\n\n```java\nNotification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)\n .setContentTitle(\"New Messages\")\n .setContentText(\"You've received 3 new messages.\")\n .setSmallIcon(R.drawable.ic_notify_status)\n .setNumber(messageCount)\n .build();\n```\n\nModify a notification's touch \\& hold menu icon\n-----------------------------------------------\n\nThe touch \\& hold menu displays the large or small icon associated with a\nnotification if available. By default, the system displays the large icon, but\nyou can call\n[`Notification.Builder.setBadgeIconType()`](/reference/androidx/core/app/NotificationCompat.Builder#setBadgeIconType(int))\nand pass in the [`BADGE_ICON_SMALL`](/reference/androidx/core/app/NotificationCompat#BADGE_ICON_SMALL())\nconstant to display the small icon. \n\n### Kotlin\n\n```kotlin\nvar notification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)\n .setContentTitle(\"New Messages\")\n .setContentText(\"You've received 3 new messages.\")\n .setSmallIcon(R.drawable.ic_notify_status)\n .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)\n .build()\n```\n\n### Java\n\n```java\nNotification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)\n .setContentTitle(\"New Messages\")\n .setContentText(\"You've received 3 new messages.\")\n .setSmallIcon(R.drawable.ic_notify_status)\n .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)\n .build();\n```\n\nHide a duplicate shortcut\n-------------------------\n\nIf your app creates a notification that duplicates an [app shortcut](/guide/topics/ui/shortcuts), you can\ntemporarily hide the shortcut while the notification is active by calling\n[`setShortcutId()`](/reference/androidx/core/app/NotificationCompat.Builder#setShortcutId(java.lang.String)).\n\nFor more sample code that uses notifications, see the [SociaLite sample app](https://github.com/android/socialite)."]]