创建展开式通知

基本通知通常包括一个标题、一行文本和 可执行哪些操作。要提供更多信息,您可以创建大型、 将多个通知模板中的其中一个作为 如本文档中所述。

首先,创建一个包含所有基本内容的通知,如 创建通知。然后, 致电 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();
<ph type="x-smartling-placeholder">
</ph> 一张图片,显示收起状态的通知和包含蓝色图片的展开通知 <ph type="x-smartling-placeholder">
</ph> 图 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();
<ph type="x-smartling-placeholder">
</ph> 一张图片,显示收起状态的通知和使用 BigTextStyle 的展开通知 <ph type="x-smartling-placeholder">
</ph> 图 2.使用 NotificationCompat.BigTextStyle

创建收件箱样式的通知

应用 NotificationCompat.InboxStyle 如果您想添加多个简短的摘要行,例如 收到的电子邮件的摘要。这样,您就可以将多段内容 每个文字都截断为一行,而不是变成一行连续的文字 由NotificationCompat.BigTextStyle提供。

要添加新行,请调用 addLine() 最多六次,如以下示例所示。如果您要添加 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();

结果如下图所示:

<ph type="x-smartling-placeholder">
</ph> 显示已展开的收件箱样式通知的图片 <ph type="x-smartling-placeholder">
</ph> 图 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();
<ph type="x-smartling-placeholder">
</ph> 以消息样式显示通知的图片 <ph type="x-smartling-placeholder">
</ph> 图 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();
<ph type="x-smartling-placeholder">
</ph> 显示带有媒体样式的通知的图片 <ph type="x-smartling-placeholder">
</ph> 图 5.使用 MediaStyleNotificationHelper.MediaStyle

其他资源

请参阅以下参考文档,详细了解 MediaStyle 和 可展开的通知。