气泡  

您所在的位置:网站首页 qq气泡显示不全 气泡  

气泡  

2023-12-01 05:58| 来源: 网络整理| 查看: 265

气泡让用户可以轻松查看并参与对话。

气泡内置于“通知”系统中。它们浮动在其他应用内容上层,并会跟随用户转到任意位置。气泡可以展开以显示应用功能和信息,并可在不使用时收起。

当设备处于已锁定状态或“显示屏始终保持开启状态”处于活动状态时,气泡就会像普通的通知那样显示。

气泡是一种可以选择停用的功能。在应用显示其第一个气泡时,系统会显示一个权限对话框,其中提供两个选项:

屏蔽来自您的应用的所有气泡 - 通知不会被屏蔽,但永远不会显示为气泡 允许来自您的应用的所有气泡 - 通过 BubbleMetaData 发送的所有通知都会显示为气泡 Bubble API

气泡是通过 Notification API 创建的,因此您可以照常发送通知。如果您希望让通知显示为气泡,则需要为其附加一些额外的数据。

气泡的展开视图是根据您选择的 Activity 创建的。此 Activity 需要经过配置才能正确显示为气泡。此 Activity 必须可以调整大小且是嵌入式的。只要 Activity 不满足其中任何一项要求,都会显示为通知。

以下代码演示了如何实现简单的气泡:

如果您的应用需要显示多个相同类型的气泡(例如与不同联系人的多个聊天对话),此 Activity 必须能够启动多个实例。在搭载 Android 10 的设备上,除非您将 documentLaunchMode 明确设置为 "always",否则通知不会显示为气泡。从 Android 11 开始,您无需明确设置此值,因为系统会自动将所有对话的 documentLaunchMode 设置为 "always"。

如需发送气泡,请按照以下步骤操作:

按照常规方式创建通知。 调用 BubbleMetadata.Builder(PendingIntent, Icon) 或 BubbleMetadata.Builder(String) 以创建 BubbleMetadata 对象。 使用 setBubbleMetadata() 将元数据添加到通知中。 如果以 Android 11 或更高版本为目标平台,对话泡元数据或通知必须引用共享快捷方式。 Kotlin // Create 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 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 bubble metadata val bubbleData = Notification.BubbleMetadata.Builder(bubbleIntent, Icon.createWithResource(context, R.drawable.icon)) .setDesiredHeight(600) .build() // Create notification, referencing the sharing shortcut val builder = Notification.Builder(context, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setBubbleMetadata(bubbleData) .setShortcutId(shortcutId) .addPerson(chatPartner) Java // Create 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 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 bubble metadata Notification.BubbleMetadata bubbleData = new Notification.BubbleMetadata.Builder(bubbleIntent, Icon.createWithResource(context, R.drawable.icon)) .setDesiredHeight(600) .build(); // Create notification, referencing the sharing shortcut Notification.Builder builder = new Notification.Builder(mContext, CHANNEL_ID) .setContentIntent(contentIntent) .setSmallIcon(smallIcon) .setBubbleMetadata(bubbleData) .setShortcutId(shortcutId) .addPerson(chatPartner); 注意:首次发送显示气泡的通知时,您必须使用具有 IMPORTANCE_MIN 或更高级别的通知渠道。

如果您的应用在发送气泡时位于前台,则系统会忽略重要程度,并始终会显示相应气泡(除非用户已屏蔽来自您应用中的气泡或通知)。

创建展开的气泡

您可以将气泡配置为自动以展开状态显示。我们建议您仅在用户执行会导致显示气泡的操作(例如点按按钮以开始新的聊天)时才使用此功能。在这种情况下,还有必要禁止显示在创建气泡时发送的初始通知。

您可以使用以下方法设置启用这些行为的标志: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_CALL,并添加了 Person。 发送通知时,应用在前台运行。

如果上述条件均不满足,系统就会显示通知而不显示气泡。

最佳做法 气泡会占用屏幕空间并遮盖其他应用内容。仅当非常需要显示气泡(例如对于进行中的通信)或用户明确要求为某些内容显示气泡时,才将通知发送为气泡。 请注意,用户可以停用气泡。在这种情况下,气泡通知会显示为一般通知。您应该始终确保您的气泡通知也可以作为一般通知使用。 从气泡启动的进程(例如 activity 和对话框)会显示在气泡容器中。这意味着气泡可以有任务堆栈。如果您的气泡中有很多功能或导航,情况就会变得很复杂。建议您尽量让功能保持具体且简明。 确保在气泡 Activity 中替换 onBackPressed 时调用 super.onBackPressed;否则,气泡可能会无法正常运行。 当气泡在收起后收到更新的消息时,气泡会显示一个标志图标,表示有未读消息。当用户在关联的应用中打开消息时,请按以下步骤操作: 更新 BubbleMetadata 以抑制通知的显示。调用 BubbleMetadata.Builder.setSupressNotification()。这会移除标志图标以表示用户处理过信息。 将 Notification.Builder.setOnlyAlertOnce() 设置为 true 可禁止涉及 BubbleMetadata 更新的声音或振动。 示例应用

People 示例应用是一个使用气泡的简单对话式应用。出于演示目的,此应用使用聊天机器人。在真实的应用中,气泡应仅用于人类发送的消息,而不用于聊天机器人发送的消息。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3