如何在 Android 11(Api 30) 上实现 Android 气泡通知
How to implement Android Bubbles notifications on Android 11(Api 30)
我正在努力执行 Android Bubbles notifications API but it's not working for me, it's displaying as an ordinary notification. I am testing on emulator API 30(Android 11). I got the people-example working on the device, and I am following the Conversation Notifications 指南。
- The notification uses MessagingStyle.
- (Only if the app targets Android 11 or higher) The notification is associated with a valid long-lived dynamic or cached sharing shortcut.
The notification can set this association by calling setShortcutId()
or setShortcutInfo(). If the app targets Android 10 or lower, the
notification doesn't have to be associated with a shortcut, as
discussed in the fallback options section.
- The user hasn't demoted the conversation from the conversation section via notification channel settings, at the time of posting.
请告诉我我错过了什么?
此外,我还有一些关于泡泡设计的可选问题。
- 我应该在应用程序的什么位置创建快捷方式以及何时更新它?
- Person 对象需要如何缓存?
这是我目前得到的
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'
我错过的一件事是为 ShortcutInfoCompat
添加 .setLongLived(true)
。它解决了这个问题。
我了解到最好在应用程序级别管理 ShortcutInfo
,因为您一次最多可以有 5 个,所以将它们缓存在内存中没有什么坏处,它包括 Person
对象也是如此。
此外,您应该添加 LocusId for the NotificationCompat
, this id is shared among shortcuts, notifications, and views. To add it to the views you need to put in some extra work, as described in ContentCaptureManager
我正在努力执行 Android Bubbles notifications API but it's not working for me, it's displaying as an ordinary notification. I am testing on emulator API 30(Android 11). I got the people-example working on the device, and I am following the Conversation Notifications 指南。
- The notification uses MessagingStyle.
- (Only if the app targets Android 11 or higher) The notification is associated with a valid long-lived dynamic or cached sharing shortcut. The notification can set this association by calling setShortcutId() or setShortcutInfo(). If the app targets Android 10 or lower, the notification doesn't have to be associated with a shortcut, as discussed in the fallback options section.
- The user hasn't demoted the conversation from the conversation section via notification channel settings, at the time of posting.
请告诉我我错过了什么?
此外,我还有一些关于泡泡设计的可选问题。
- 我应该在应用程序的什么位置创建快捷方式以及何时更新它?
- Person 对象需要如何缓存?
这是我目前得到的
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'
我错过的一件事是为 ShortcutInfoCompat
添加 .setLongLived(true)
。它解决了这个问题。
我了解到最好在应用程序级别管理 ShortcutInfo
,因为您一次最多可以有 5 个,所以将它们缓存在内存中没有什么坏处,它包括 Person
对象也是如此。
此外,您应该添加 LocusId for the NotificationCompat
, this id is shared among shortcuts, notifications, and views. To add it to the views you need to put in some extra work, as described in ContentCaptureManager