android - 通知气泡未显示
问题描述
出于某种原因,每当我发送通知时,通知气泡都不会出现。我允许来自开发人员选项的通知气泡,compileSdkVersion 是 29。气泡活动中只有一个按钮用于测试目的。任何帮助将不胜感激。
我的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NotificationManager notificationManager = mContext.getSystemService(NotificationManager.class);
// set up notification channel
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(CHANNEL_DESCRIPTION);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
// create person
Person person = new Person.Builder()
.setName("Notification")
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
.setBot(true)
.setImportant(true)
.build();
// create bubble meta data
Intent target = new Intent(mContext, BubbleActivity.class);
PendingIntent bubbleIntent = PendingIntent.getActivity(mContext, 0, target, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.BubbleMetadata bubbleMetadata = new Notification.BubbleMetadata.Builder()
.setDesiredHeight(300)
.setIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
.setSuppressNotification(true)
.setAutoExpandBubble(false)
.setIntent(bubbleIntent)
.build();
// create message style
Notification.MessagingStyle style = new Notification.MessagingStyle(person)
.setGroupConversation(false)
.addMessage("notification", currentTimeMillis(), person);
// create notification intent
Intent target2 = new Intent(mContext.getApplicationContext(), LaunchActivity.class);
PendingIntent notificationIntent = PendingIntent.getActivity(mContext.getApplicationContext(), 1, target2, PendingIntent.FLAG_UPDATE_CURRENT);
// create notification
Notification notification = new Notification.Builder(mContext, CHANNEL_ID)
.setContentTitle("Notification")
.setSmallIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
.setLargeIcon(Icon.createWithResource(mContext, R.drawable.ic_notification))
.setCategory(Notification.CATEGORY_MESSAGE)
.setStyle(style)
.setBubbleMetadata(bubbleMetadata)
.addPerson(person)
.setShowWhen(true)
.setContentIntent(notificationIntent)
.build();
if (notificationManager != null) {
notificationManager.notify(0, notification);
}
}
解决方案
您需要在通知上设置快捷方式 ID。完成后,您的通知将出现在对话空间中,并允许冒泡。请注意,气泡最初不会自动冒泡。由用户调用/触发它。
请参考以下资源: https ://developer.android.com/guide/topics/ui/conversations#api-shortcuts
推荐阅读
- excel - 文本到列 (VBA) 问题
- android - 如何使用绑定从 Fragment 访问 Activity 中的 BottomAppBar?
- python - 如何检查某些元素是否在python的代码中
- laravel-backpack - 如何在 laravel 背包中自定义取消按钮?
- swagger - 如何在@nestjs/swagger 中使用 urls.primaryName
- python-3.x - 将 Pandas DataFrame 重塑为以索引为前缀的列的系列
- apache-kafka - 使用 Spring Cloud Stream Kafka Binder 在批处理功能中发布多条消息
- wordpress - 在 WooCommerce 之外使用 Timber、Twig 和 ACF 获取 WooCommerce 产品的产品价格
- node.js - 如何使用创建用户或其他 API 在 keycloak 中添加用户同意?
- flutter - 按下后退按钮时,应用程序会颤抖关闭吗?