展開可能な通知を作成する

基本的な通知には通常、タイトル、テキスト行、それに応じてユーザーが実行できるアクションが含まれます。さらに詳しい情報を提供するには、このドキュメントで説明するように複数の通知テンプレートのいずれかを適用することで、大きな展開可能な通知を作成します。

まず、通知を作成するで説明されているように、すべての基本コンテンツを含む通知を作成します。次に、次の例に示すように、スタイル オブジェクトを指定して setStyle() を呼び出し、各テンプレートに対応する情報を指定します。

大きな画像を追加する

通知に画像を追加するには、NotificationCompat.BigPictureStyle のインスタンスを setStyle() に渡します。

Kotlin

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_post)
        .setContentTitle(imageTitle)
        .setContentText(imageDescription)
        .setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(myBitmap))
        .build()

Java

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_post)
        .setContentTitle(imageTitle)
        .setContentText(imageDescription)
        .setStyle(new NotificationCompat.BigPictureStyle()
               .bigPicture(myBitmap))
        .build();

通知が折りたたまれているときに画像をサムネイルとしてのみ表示するには、次の図に示すように、setLargeIcon() を呼び出して画像を渡します。次に、BigPictureStyle.bigLargeIcon() を呼び出して null を渡し、通知が展開されたときに大きなアイコンが非表示になるようにします。

Kotlin

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_post)
        .setContentTitle(imageTitle)
        .setContentText(imageDescription)
        .setLargeIcon(myBitmap)
        .setStyle(NotificationCompat.BigPictureStyle()
                .bigPicture(myBitmap)
                .bigLargeIcon(null))
        .build()

Java

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_post)
        .setContentTitle(imageTitle)
        .setContentText(imageDescription)
        .setLargeIcon(myBitmap)
        .setStyle(new NotificationCompat.BigPictureStyle()
                .bigPicture(myBitmap)
                .bigLargeIcon(null))
        .build();
閉じられた通知と青い画像を含む開かれた通知を示す画像
図 1.NotificationCompat.BigPictureStyle を使用した通知。

大きなテキスト ブロックを追加する

NotificationCompat.BigTextStyle を適用して、通知の展開済みコンテンツ領域にテキストを表示します。

Kotlin

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_mail)
        .setContentTitle(emailObject.getSenderName())
        .setContentText(emailObject.getSubject())
        .setLargeIcon(emailObject.getSenderAvatar())
        .setStyle(NotificationCompat.BigTextStyle()
                .bigText(emailObject.getSubjectAndSnippet()))
        .build()

Java

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_mail)
        .setContentTitle(emailObject.getSenderName())
        .setContentText(emailObject.getSubject())
        .setLargeIcon(emailObject.getSenderAvatar())
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(emailObject.getSubjectAndSnippet()))
        .build();
閉じた通知と BigTextStyle を使用して開いた通知を示す画像
図 2.NotificationCompat.BigTextStyle を使用した通知。

受信トレイスタイルの通知を作成する

受信メールのスニペットなど、複数の短い概要行を追加する場合は、通知に NotificationCompat.InboxStyle を適用します。これにより、NotificationCompat.BigTextStyle で提供される連続する 1 行のテキストではなく、1 行で切り捨てられた複数のコンテンツ テキストを追加できます。

新しい行を追加するには、次の例に示すように addLine() を最大 6 回呼び出します。6 行以上追加すると、最初の 6 行のみが表示されます。

Kotlin

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.baseline_email_24)
        .setContentTitle("5 New mails from Frank")
        .setContentText("Check them out")
        .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.logo))
        .setStyle(
                NotificationCompat.InboxStyle()
                .addLine("Re: Planning")
                .addLine("Delivery on its way")
                .addLine("Follow-up")
        )
        .build()

Java

Notification notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.baseline_email_24)
        .setContentTitle("5 New mails from Frank")
        .setContentText("Check them out")
        .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.logo))
        .setStyle(
                NotificationCompat.InboxStyle()
                .addLine("Re: Planning")
                .addLine("Delivery on its way")
                .addLine("Follow-up")
        )
        .build();

結果は次の図のようになります。

展開された受信トレイ形式の通知を示す画像
図 3.展開された受信トレイ形式の通知。

通知に会話を表示する

NotificationCompat.MessagingStyle を適用して、任意の数の人間の連続したメッセージを表示します。これはメッセージ アプリに最適です。送信者名とメッセージ テキストを個別に処理することで各メッセージのレイアウトに一貫性を持たせることができ、各メッセージは複数行でも構いません。

新しいメッセージを追加するには、addMessage() を呼び出して、メッセージ テキスト、受信時刻、送信者の名前を渡します。次の例に示すように、この情報を NotificationCompat.MessagingStyle.Message オブジェクトとして渡すこともできます。

Kotlin

val message1 = NotificationCompat.MessagingStyle.Message(
        messages[0].getText(),
        messages[0].getTime(),
        messages[0].getSender())
val message2 = NotificationCompat.MessagingStyle.Message(
        messages[1].getText(),
        messages[1].getTime(),
        messages[1].getSender())
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_message)
        .setStyle(
                NotificationCompat.MessagingStyle(resources.getString(R.string.reply_name))
                .addMessage(message1)
                .addMessage(message2))
        .build()

Java

NotificationCompat.MessagingStyle.Message message1 =
        new NotificationCompat.MessagingStyle.Message(messages[0].getText(),
                                                      messages[0].getTime(),
                                                      messages[0].getSender());
NotificationCompat.MessagingStyle.Message message2 =
        new NotificationCompat.MessagingStyle.Message(messages[1].getText(),
                                                      messages[1].getTime(),
                                                      messages[1].getSender());

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_message)
        .setStyle(new NotificationCompat.MessagingStyle(resources.getString(R.string.reply_name))
                .addMessage(message1)
                .addMessage(message2))
        .build();
メッセージ スタイルの通知を示す画像
図 4.NotificationCompat.MessagingStyle を使用した通知。

NotificationCompat.MessagingStyle を使用する場合、setContentTitle()setContentText() に指定された値は無視されます。

setConversationTitle() を呼び出すと、会話の上にタイトルを追加できます。ユーザーが作成したグループの名前か、特定の名前がない場合は会話の参加者のリストになります。1 対 1 のチャットの会話のタイトルは設定しないでください。このフィールドの有無が会話がグループであることを示すヒントとして使用されます。

このスタイルは、Android 7.0(API レベル 24)以降を搭載しているデバイスにのみ適用されます。互換性ライブラリ(NotificationCompat)を使用する場合、前述のように、MessagingStyle を含む通知は、サポートされている展開型通知スタイルに自動的にフォールバックします。

チャットの会話に対してこのような通知を作成する場合は、直接返信アクションを追加します。

メディア コントロールを備えた通知を作成する

MediaStyleNotificationHelper.MediaStyle を適用して、メディア再生コントロールとトラック情報を表示します。

関連する MediaSession をコンストラクタに指定します。これにより、Android はメディアに関する正しい情報を表示できます。

addAction() を最大 5 回呼び出して、最大 5 つのアイコンボタンを表示します。setLargeIcon() を呼び出してアルバム アートワークを設定します。

他の通知スタイルとは異なり、MediaStyle では、折りたたみビューにも表示される 3 つのアクション ボタンを指定することで、折りたたみサイズ コンテンツ ビューを変更できます。そのためには、アクション ボタンのインデックスを setShowActionsInCompactView() に指定します。

次の例は、メディア コントロールを使用した通知を作成する方法を示しています。

Kotlin

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        // Show controls on lock screen even when user hides sensitive content.
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setSmallIcon(R.drawable.ic_stat_player)
        // Add media control buttons that invoke intents in your media service
        .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
        .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1
        .addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2
        // Apply the media style template.
        .setStyle(MediaStyleNotificationHelper.MediaStyle(mediaSession)
                .setShowActionsInCompactView(1 /* #1: pause button \*/))
        .setContentTitle("Wonderful music")
        .setContentText("My Awesome Band")
        .setLargeIcon(albumArtBitmap)
        .build()

Java

Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
        // Show controls on lock screen even when user hides sensitive content.
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setSmallIcon(R.drawable.ic_stat_player)
        // Add media control buttons that invoke intents in your media service
        .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
        .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)  // #1
        .addAction(R.drawable.ic_next, "Next", nextPendingIntent)     // #2
        // Apply the media style template.
        .setStyle(new MediaStyleNotificationHelper.MediaStyle(mediaSession)
                .setShowActionsInCompactView(1 /* #1: pause button */))
        .setContentTitle("Wonderful music")
        .setContentText("My Awesome Band")
        .setLargeIcon(albumArtBitmap)
        .build();
メディア スタイル付きの通知を示す画像
図 5.MediaStyleNotificationHelper.MediaStyle を使用した通知。

参考情報

MediaStyle と展開可能な通知の詳細については、以下のリファレンスをご覧ください。