1

我正在尝试实现Android Bubbles 通知 API,但它对我不起作用,它显示为普通通知。我正在模拟器 API 30(Android 11)上进行测试。我得到了在设备上工作的人员示例,并且我正在遵循对话通知指南。

  • 通知使用 MessagingStyle。
  • (仅当应用面向 Android 11 或更高版本时)通知与有效的长期动态或缓存的共享快捷方式相关联。通知可以通过调用 setShortcutId() 或 setShortcutInfo() 来设置此关联。如果应用面向 Android 10 或更低版本,则通知不必与快捷方式相关联,如后备选项部分所述。
  • 在发布时,用户尚未通过通知渠道设置将对话从对话部分降级。

请告诉我我错过了什么?

此外,我还收到了一些关于 Bubbles 设计的可选问题。

  • 我应该在应用程序的什么时候创建快捷方式以及何时更新它?
  • 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" />

摇篮

targetSDKVersion 30
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
4

1 回答 1

1

我错过的一件事是.setLongLived(true)ShortcutInfoCompat. 它解决了这个问题。

我了解到最好ShortcutInfo在应用程序级别进行管理,因为一次最多可以有 5 个,因此将它们缓存在内存中没有害处,它也包括Person对象。

此外,您应该为 . 添加LocusId NotificationCompat此 id 在快捷方式、通知和视图之间共享。要将其添加到视图中,您需要进行一些额外的工作,如ContentCaptureManager中所述

于 2020-11-23T15:38:56.360 回答