气泡可让用户更轻松地查看对话和参与对话。
气泡内置于通知系统中。此类气泡会悬浮在其他应用内容之上,并会随用户一起移动。用户可以展开气泡以显示应用功能和信息,并在不使用时将其收起。
当设备处于锁定状态或“屏幕常亮”处于活动状态时,气泡像通知一样显示。
气泡是一种可以选择停用的功能。当应用显示第一个气泡时,权限对话框提供两个选项:
- 屏蔽来自您的应用的所有气泡。通知不会被屏蔽,但绝不会以气泡的形式显示。
- 允许来自您的应用的所有气泡。通过
BubbleMetaData
发送的所有通知均显示为气泡。
bubble API
气泡是通过通知 API 创建的,因此请照常发送通知。如果您希望通知显示为气泡,请向其附加额外的数据。
气泡的展开视图是根据您选择的 activity 创建的。配置 activity,使其正确显示为气泡。此 activity 必须可调整大小且是嵌入式的。如果它不满足其中任一要求,便会改为以通知的形式显示。
以下代码演示了如何实现简单的气泡:
<activity
android:name=".bubbles.BubbleActivity"
android:theme="@style/AppTheme.NoActionBar"
android:label="@string/title_activity_bubble"
android:allowEmbedded="true"
android:resizeableActivity="true"
/>
如果应用显示多个相同类型的气泡(例如与不同联系人的多个聊天对话),则此 activity 必须能够启动多个实例。在搭载 Android 10 及更低版本的设备上,除非您将 documentLaunchMode
明确设置为 "always"
,否则通知不会显示为气泡。从 Android 11 开始,您无需明确设置此值,因为系统会自动将所有对话的 documentLaunchMode
设置为 "always"
。
如需发送气泡,请按照以下步骤操作:
- 像往常一样创建通知。
- 调用
BubbleMetadata.Builder(PendingIntent, Icon)
或BubbleMetadata.Builder(String)
以创建BubbleMetadata
对象。 - 使用
setBubbleMetadata()
将元数据添加到通知中。 - 如果以 Android 11 或更高版本为目标平台,请确保气泡元数据或通知引用共享快捷方式。
这些步骤如以下示例所示:
Kotlin
// Create a bubble intent. val target = Intent(context, BubbleActivity::class.java) val bubbleIntent = PendingIntent.getActivity(context, 0, target, 0 /* flags */) val category = "com.example.category.IMG_SHARE_TARGET" val chatPartner = Person.Builder() .setName("Chat partner") .setImportant(true) .build() // Create a sharing shortcut. val shortcutId = generateShortcutId() val shortcut = ShortcutInfo.Builder(mContext, shortcutId) .setCategories(setOf(category)) .setIntent(Intent(Intent.ACTION_DEFAULT)) .setLongLived(true) .setShortLabel(chatPartner.name) .build() // Create a bubble metadata. val bubbleData = Notification.BubbleMetadata.Builder(bubbleIntent, Icon.createWithResource(context, R.drawable.icon)) .setDesiredHeight(600) .build() // Create a notification, referencing the sharing shortcut. val builder = Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setBubbleMetadata(bubbleData) .setShortcutId(shortcutId) .addPerson(chatPartner)
Java
// Create a bubble intent. Intent target = new Intent(mContext, BubbleActivity.class); PendingIntent bubbleIntent = PendingIntent.getActivity(mContext, 0, target, 0 /* flags */); private val CATEGORY_TEXT_SHARE_TARGET = "com.example.category.IMG_SHARE_TARGET" Person chatPartner = new Person.Builder() .setName("Chat partner") .setImportant(true) .build(); // Create a sharing shortcut. private String shortcutId = generateShortcutId(); ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, shortcutId) .setCategories(Collections.singleton(CATEGORY_TEXT_SHARE_TARGET)) .setIntent(Intent(Intent.ACTION_DEFAULT)) .setLongLived(true) .setShortLabel(chatPartner.getName()) .build(); // Create a bubble metadata. Notification.BubbleMetadata bubbleData = new Notification.BubbleMetadata.Builder(bubbleIntent, Icon.createWithResource(context, R.drawable.icon)) .setDesiredHeight(600) .build(); // Create a notification, referencing the sharing shortcut. Notification.Builder builder = new Notification.Builder(mContext, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setBubbleMetadata(bubbleData) .setShortcutId(shortcutId) .addPerson(chatPartner);
如果您的应用在发送气泡时位于前台,系统会忽略重要性,并始终显示气泡,除非用户屏蔽来自您的应用的气泡或通知。
创建展开的气泡
您可以将气泡配置为自动以展开状态显示。我们建议仅在用户执行导致显示气泡的操作(例如点按按钮以开始新聊天)时才使用此功能。在这种情况下,还有必要禁止显示创建气泡时发送的初始通知。
您可以使用以下方法设置启用这些行为的标志:setAutoExpandBubble()
和 setSuppressNotification()
。
以下示例展示了如何将气泡配置为自动以展开状态显示:
Kotlin
val bubbleMetadata = Notification.BubbleMetadata.Builder() .setDesiredHeight(600) .setIntent(bubbleIntent) .setAutoExpandBubble(true) .setSuppressNotification(true) .build()
Java
Notification.BubbleMetadata bubbleData = new Notification.BubbleMetadata.Builder() .setDesiredHeight(600) .setIntent(bubbleIntent) .setAutoExpandBubble(true) .setSuppressNotification(true) .build();
气泡内容生命周期
当气泡展开时,内容 activity 会经历正常的进程生命周期,这会导致应用成为前台进程(如果尚未在前台进程)。
当气泡收起或关闭时,activity 会被销毁。这可能会导致进程被缓存并在稍后终止,具体取决于应用是否有其他前台组件正在运行。
何时显示气泡
为减少对用户的干扰,消息气泡仅在某些情况下显示。
如果应用以 Android 11 或更高版本为目标平台,那么除非通知满足对话要求,否则不会以气泡形式显示。如果应用以 Android 10 或更低版本为目标平台,则只有在满足以下一个或多个条件时,通知才会显示为气泡:
- 通知使用
MessagingStyle
并添加了Person
。 - 通知来自对
Service.startForeground
的调用,category
为CATEGORY_CALL
,并且添加了Person
。 - 发送通知时,应用在前台运行。
如果上述条件均不满足,系统会显示通知而不是以消息气泡形式显示。
最佳实践
- 仅当较为重要时(例如,作为持续通信的一部分,或用户明确请求以气泡形式显示内容时),才以气泡形式发送通知。气泡会占用屏幕空间并遮盖其他应用内容。
- 确保您的消息气泡通知也可以作为普通通知使用。当用户停用气泡后,气泡通知会以常规通知的形式显示。
- 尽量确保功能具体且轻量。从气泡启动的进程(例如 activity 和对话框)显示在气泡容器中。这意味着气泡可以有任务堆栈。如果您的气泡中有很多功能或导航,情况就会变得很复杂。
- 在气泡 activity 中替换
onBackPressed
时,调用super.onBackPressed
。否则,消息气泡可能无法正常运行。
当收起的气泡收到更新的消息时,气泡会显示一个标记图标,表示有未读消息。当用户在关联的应用中打开消息时,请按以下步骤操作:
- 更新
BubbleMetadata
以抑制通知的显示。调用BubbleMetadata.Builder.setSuppressNotification()
。此操作会移除标记图标,以表明用户与邮件进行了互动。 - 将
Notification.Builder.setOnlyAlertOnce()
设置为true
可抑制随BubbleMetadata
更新一起发出的声音或振动。
示例应用
People 示例应用是一个使用对话泡的简单对话应用。出于演示目的,此应用使用聊天机器人。在实际应用中,只能对真人(而非机器人)发送的消息使用对话泡。