创建可扩展通知

基本通知通常包括标题、一行文本以及用户可以执行的操作以进行响应。为了提供更多信息,您可以通过应用本文档中描述的几种通知模板之一来创建大型的可扩展通知。

首先,按照创建通知中所述构建包含所有基本内容的通知。然后,使用样式对象调用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();
An image showing a collapsed notification and an expanded notification containing a blue image
图 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();
An image showing a collapsed notification and an expanded notification using BigTextStyle
图 2. 使用NotificationCompat.BigTextStyle的通知。

创建收件箱样式通知

如果要添加多行简短摘要行(例如来自传入电子邮件的摘要),请将NotificationCompat.InboxStyle应用于通知。这使您可以添加多段内容文本,每段文本都被截断为一行,而不是NotificationCompat.BigTextStyle提供的一行连续文本。

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

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

结果如下所示

An image showing an expanded inbox-style notification
图 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();
An image showing 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()

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

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();
An image showing a notification with media style
图 5. 使用MediaStyleNotificationHelper.MediaStyle的通知。

其他资源

有关MediaStyle和可扩展通知的更多信息,请参阅以下参考。