通知バッジを変更する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
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 つ増えますが、アプリでこの数をオーバーライドすることができます。たとえば、複数の新着メッセージに対して 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 UTC。
[null,null,["最終更新日 2025-07-27 UTC。"],[],[],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)."]]