特定の状況では、アプリで継続的なアラームや着信などを使用して、ユーザーの注意を早急に引き付けることが必要になります。Android 9(API レベル 28)以前を搭載するデバイスをターゲットとするアプリでは、アプリがバックグラウンドにあるときにアクティビティを起動することで、この問題に対処できます。このドキュメントでは、 この動作は、Android 10(API レベル 29)を搭載したデバイスでの Android 13(API レベル 33)。
POST_NOTIFICATIONS 権限を追加する
Android 13 以降では、次の行を
AndroidManifest.xml
ファイル:
<manifest ...> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <application ...> ... </application> </manifest>
作成したら、通知チャンネルを作成できます。
通知チャネルを作成する
通知チャンネルを作成して通知を適切に表示し、ユーザーがアプリの設定で通知を管理できるようにします。詳細については、このモジュールの 通知チャンネルについて詳しくは、通知の作成と管理 。
Application
クラスの通知チャンネルを作成する
onCreate
メソッド:
Kotlin
class DACapp : Application() { override fun onCreate() { super.onCreate() val channel = NotificationChannel( CHANNEL_ID, "High priority notifications", NotificationManager.IMPORTANCE_HIGH ) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) } }
ユーザーがアプリを初めて実行すると、アプリのアプリ情報システム画面に図 1 のような画面が表示されます。
![アプリの [アプリ情報]、[通知] 画面を示す画像。](https://developer.android.google.cn/static/images/ui/notifications/time-sensitive_notification_channel_empty.png?authuser=0&hl=ja)
通知の権限を管理する
Android 13 以降では、次の日付より前に通知権限をリクエストしてください。 ユーザーに通知を表示します
最小限の実装は次のとおりです。
Kotlin
val permissionLauncher = rememberLauncherForActivityResult( contract = ActivityResultContracts.RequestPermission(), onResult = { hasNotificationPermission = it } ) ... Button( onClick = { if (!hasNotificationPermission) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) } } }, ) { Text(text = "Request permission") }
Android 13 を搭載したデバイスの場合、Request
permission
ボタンをタップすると、図 2 に示すダイアログが表示されます。

ユーザーが権限リクエストを承認すると、アプリの [アプリ情報] セクションは図 3 のようになります。
![通知権限リクエストが許可された後の [アプリ情報] 画面の画像](https://developer.android.google.cn/static/images/ui/notifications/time-sensitive_notification_permission_granted.png?authuser=0&hl=ja)
高優先度通知を作成する
通知を作成する際は、わかりやすいタイトルとメッセージを指定します。
次の例には通知が含まれています。
Kotlin
private fun showNotification() { val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.baseline_auto_awesome_24) .setContentTitle("HIGH PRIORITY") .setContentText("Check this dog puppy video NOW!") .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_RECOMMENDATION) notificationManager.notify(666, notificationBuilder.build()) }
通知をユーザーに表示する
showNotification()
関数を呼び出すと、次のように通知がトリガーされます。
Kotlin
Button(onClick = { showNotification() }) { Text(text = "Show notification") }
この例の通知は図 4 のようになります。

進行中の通知
通知をユーザーに表示するとき、ユーザーは通知を確認または拒否できます 追加できますたとえば、ユーザーは電話の着信を受け入れるか拒否するかを選択できます。
電話の着信など、進行中の通知の場合は、 その通知をフォアグラウンド サービス。次のコード スニペットは、フォアグラウンド サービスに関連付けられた通知を表示する方法を示しています。
Kotlin
// Provide a unique integer for the "notificationId" of each notification. startForeground(notificationId, notification)
Java
// Provide a unique integer for the "notificationId" of each notification. startForeground(notificationId, notification);