修改通知標記
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
從 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,如圖 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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)."]]