创建展开式通知

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

首先,请按照创建通知中的说明构建包含所有基本内容的通知。然后,使用样式对象调用 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()

收起的通知和展开的通知(包含蓝色图片)
图 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()

收起和展开后的通知(使用 BigTextStyle)
图 2. 使用 NotificationCompat.BigTextStyle 的通知。

创建收件箱样式的通知

如果您想添加多行简短的摘要,例如来自收到的电子邮件的摘要,请将 NotificationCompat.InboxStyle 应用于通知。这样一来,您就可以添加多段内容文字,每段文字都会被截断为一行,而不是像 NotificationCompat.BigTextStyle 那样提供一行连续的文字。

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

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()

结果如图所示:

展开式收件箱风格的通知
图 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()

消息样式的通知
图 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()

具有媒体样式的通知
图 5. 使用 MediaStyleNotificationHelper.MediaStyle 的通知。

其他资源

如需详细了解 MediaStyle 和可展开的通知,请参阅以下参考资料。