我正在尝试实现Android泡泡通知API,但它并不适用于我,它显示为一个普通的通知。我正在仿真器API 30(Android 11)上进行测试。我让人-榜样在设备上工作,我正在遵循会话通知准则。
请告诉我我错过了什么?
另外,我还有一些关于气泡设计的可选问题。
这就是我到目前为止所得到的
Recipient recipient = ...; // Sender data
Message message = ...; // Message data
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(ChatActivity.CONVERSATION_ID, message.conversationId);
PendingIntent bubbleIntent =
PendingIntent.getActivity(context, 0, intent, 0);
IconCompat icon = loadIcon(recipient);
Person person = loadPerson(recipient, icon);
NotificationCompat.MessagingStyle style = getMessagingStyle(person);
createOrVerifyChannel();
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(getNewMessagesCount(message) + " new messages with " + person.getName())
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentText(message.text)
.setBubbleMetadata(
new NotificationCompat.BubbleMetadata.Builder()
.setDesiredHeight(600)
.setIntent(bubbleIntent)
.setAutoExpandBubble(true)
.setSuppressNotification(true)
.setIcon(icon)
.build()
)
.addPerson(person)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setWhen(message.date)
.setStyle(style)
.setShortcutInfo(
new ShortcutInfoCompat.Builder(context, message.conversationId + "")
.setActivity(new ComponentName(context, ChatActivity.class))
.setCategories(new HashSet<>(Collections.singletonList(Notification.CATEGORY_MESSAGE)))
.setIcon(icon)
.setPerson(person)
.setRank(0)
.setShortLabel(person.getName())
.setIntent(intent)
.build()
)
.build();
NotificationManagerCompat.from(context).notify(message.id + "," + message.type,
message.id, notification);
清单
<activity
android:name=".screens.chat.ChatActivity"
android:allowEmbedded="true"
android:resizeableActivity="true"
tools:targetApi="n" />
Gradle
targetSDKVersion 30
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
发布于 2020-11-23 15:38:56
我错过的一件事是为.setLongLived(true)
添加了ShortcutInfoCompat
。它解决了问题。
我了解到,最好在应用程序级别上管理ShortcutInfo
,因为一次最多可以有5次,所以缓存内存中的内容没有坏处,它还包括Person
对象。
另外,您应该为LocusId添加NotificationCompat
,这个id在快捷方式、通知和视图之间共享。要将其添加到视图中,需要添加一些额外的工作,如ContentCaptureManager中所述
https://stackoverflow.com/questions/64938421
复制相似问题