创建可展开的通知

基本通知通常包含标题、一行文本以及用户可以执行的响应操作。如需提供更多信息,您可以应用本文档中介绍的几种通知模板之一,创建大型的可展开通知。

首先,按照 创建通知 中的说明构建包含所有基本内容的通知。然后,调用 setStyle() 并传入一个样式对象,并提供与各模板对应的信息,如以下示例所示。

添加大图

要在通知中添加图片,请将 NotificationCompat.BigPictureStyle 的实例传递给 setStyle()

var notification =
    NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(com.example.compose.snippets.R.drawable.ic_logo)
        .setContentTitle("Title")
        .setContentText("Content text")
        .setStyle(
            NotificationCompat.BigPictureStyle()
                .bigPicture(bitmapImage)
        )
        .build()

若要使图片仅在通知折叠时显示为缩略图(如下图所示),请调用 setLargeIcon() 并传入该图片。然后,调用 BigPictureStyle.bigLargeIcon() 并传入 null,这样当通知展开时,大图标就会消失。

notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_logo)
    .setContentTitle("Title")
    .setContentText("Content text")
    .setLargeIcon(Icon.createWithResource(context, R.drawable.dog))
    .setStyle(
        NotificationCompat.BigPictureStyle()
            .bigPicture(bitmapImage)
            .bigLargeIcon(null as Bitmap?)
    )
    .build()

A collapsed notification and an expanded notification containing a blue image
图 1. 使用 NotificationCompat.BigPictureStyle 的通知。

添加大段文本

应用 NotificationCompat.BigTextStyle 可在通知的展开内容区域显示文本。

notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_logo)
    .setContentTitle("Sender name")
    .setContentText("Email subject")
    .setLargeIcon(Icon.createWithResource(context, R.drawable.dog))
    .setStyle(
        NotificationCompat.BigTextStyle()
            .bigText(someVeryLongMessage)
    )
    .build()

A collapsed notification and an expanded notification using BigTextStyle
图 2. 使用 NotificationCompat.BigTextStyle 的通知。

创建收件箱风格的通知

如果您想添加多行简短摘要(例如来自收到邮件的片段),请将 NotificationCompat.InboxStyle 应用于通知。与 NotificationCompat.BigTextStyle 提供的一长串连续文本不同,这允许您添加多段内容文本,每段文本都会被截断为一行。

如需添加新行,请调用 addLine() 最多六次,如以下示例所示。如果您添加超过六行,则仅显示前六行。

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

结果如下图所示:

An expanded inbox-style notification
图 3. 展开后的收件箱风格通知。

在通知中显示对话

应用 NotificationCompat.MessagingStyle 以显示任意人数之间的连续消息。这对于通讯类应用非常理想,因为它通过分别处理发送者姓名和消息文本,为每条消息提供了统一的布局,且每条消息都可以包含多行文本。

如需添加新消息,请调用 addMessage(),并传入消息文本、接收时间和发送者姓名。您也可以将此信息作为 NotificationCompat.MessagingStyle.Message 对象传入,如以下示例所示。

val message1 = NotificationCompat.MessagingStyle.Message(
    messages[0].text,
    messages[0].time,
    messages[0].sender
)
val message2 = NotificationCompat.MessagingStyle.Message(
    messages[1].text,
    messages[1].time,
    messages[1].sender
)
notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_logo)
    .setStyle(
        NotificationCompat.MessagingStyle(Person.Builder().setName("Me").build())
            .addMessage(message1)
            .addMessage(message2)
    )
    .build()

A notification in messaging style
图 4. 使用 NotificationCompat.MessagingStyle 的通知。

使用 NotificationCompat.MessagingStyle 时,提供给 setContentTitle()setContentText() 的任何值都将被忽略。

您可以调用 setConversationTitle() 来添加显示在对话上方的标题。这可以是用户创建的群组名称;如果群组没有特定名称,则可以是对话参与者的列表。不要为一对一聊天设置对话标题,因为系统会将该字段的存在视为该对话为群聊的提示。

此样式仅适用于运行 Android 7.0 (API 级别 24) 及更高版本的设备。如前所述,当使用兼容性库 (NotificationCompat) 时,带有 MessagingStyle 的通知会自动回退到受支持的展开式通知样式。

为聊天对话构建此类通知时,请 添加直接回复操作

创建带有媒体控件的通知

应用 MediaStyleNotificationHelper.MediaStyle 以显示媒体播放控件和曲目信息。

在构造函数中指定关联的 MediaSession。这允许 Android 显示有关您媒体的正确信息。

调用 addAction() 最多五次以显示最多五个图标按钮。调用 setLargeIcon() 以设置专辑封面。

与其他通知样式不同,MediaStyle 还允许您通过指定三个也会出现在折叠视图中的操作按钮来修改折叠后的内容视图。为此,请将操作按钮的索引提供给 setShowActionsInCompactView()

以下示例展示了如何创建带有媒体控件的通知:

notification = NotificationCompat.Builder(context, CHANNEL_ID)
    // Show controls on lock screen even when user hides sensitive content.
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setSmallIcon(com.example.compose.snippets.R.drawable.play)
    // Add media control buttons that invoke intents in your media service
    .addAction(R.drawable.previous, "Previous", null /* Add valid intent */) // #0
    .addAction(R.drawable.pause, "Pause", null /* Add valid intent */) // #1
    .addAction(R.drawable.next, "Next", null /* Add valid intent */) // #2
    // Apply the media style template.
    .setStyle(MediaStyleNotificationHelper.MediaStyle(mediaSession)
        .setShowActionsInCompactView(1 /* #1: pause button */))
    .setContentTitle("Wonderful music")
    .setContentText("My Awesome Band")
    .setLargeIcon(bitmapImage)
    .build()

A notification with media style
图 5. 使用 MediaStyleNotificationHelper.MediaStyle 的通知。

其他资源

有关 MediaStyle 和可展开通知的更多信息,请参阅以下参考资料: