通知消息样式存储消息的位置

问题描述 投票:0回答:3

所以我正在构建一个消息应用程序,我尝试使用NotificationCompat.MessagingStyle作为我的通知,它通常适用于一条消息,但添加的每条消息只是替换最后一条消息,而不是显示收到的每条消息,直到清除或查看(期望的结果),查看他们说添加这样的消息的文档

.addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
.addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())

这让我假设他们将这些消息存储在数组中的某个地方,但我不确定在哪里实现它,就像我在同一个类中创建一个新数组一样,我假设如果将其保存到共享中,结果将是相同的首选项(似乎有点占用内存)或者可能在未读消息的小型数据库中?我已经在消息中存储了一个值,以确定它们是否已发送或接收,我可以对所有未读消息运行查询,填充列表并迭代它,尽管这似乎有点过头了?

更新

仍然没有任何进展,尝试制作一个消息列表并像这样添加它们,但仍然没有乐趣

NotificationCompat.MessagingStyle messagingStyle = new 
NotificationCompat.MessagingStyle(message.getRecipientName());         
messagingStyle.setConversationTitle(
getSmsTodayYestFromMilli(message.getTime_stamp().getTime()));
if (messages != null)
   for(DatabaseMessage dbMessage : messages){         
           messagingStyle.addMessage(dbMessage.getMessage(),
           dbMessage.getTime_stamp().getTime(),dbMessage.getRecipientName());
      }
   }
notificationBuilder.setStyle(messagingStyle);
android push-notification firebase-cloud-messaging
3个回答
6
投票

我知道这是一个老问题,但这对某些人来说可能有用。

您可以从现有通知中提取之前的消息:

NotificationCompat.MessagingStyle style = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(existingNotification);
style.getMessages();

3
投票

所以我找到了如何做到这一点,但仍然不确定在哪里存储列表,但要添加一条消息,您实际上需要创建一个

NotificationCompat.MessagingStyle.Message
然后使用
addMessage
方法,现在我正在尝试获取所有未读消息的列表并对其进行迭代,但它仍然存在问题,但我认为这些问题需要一个新问题,这里是添加消息:

NotificationCompat.MessagingStyle messagingStyle = new 
NotificationCompat.MessagingStyle(message.getRecipientName());
        messagingStyle.setConversationTitle(getSmsTodayYestFromMilli(
        message.getTime_stamp().getTime()));
        if (messages != null){
            Log.d(MYTAG,"list size " + messages.size());
            for(DatabaseMessage databaseMessage : messages){
                NotificationCompat.MessagingStyle.Message notificationMessage  = new 
                NotificationCompat.MessagingStyle.Message(
                        databaseMessage.getMessage(),
                        databaseMessage.getTime_stamp().getTime(),
                        databaseMessage.getRecipientName()
                );
                messagingStyle.addMessage(notificationMessage);
            }
        }

notificationBuilder.setStyle(messagingStyle);

0
投票

此答案的另一个更新。与上面的答案类似,但现在有一种特定的方法来提取消息传递样式,而不是通用样式

    val manager = context.getSystemService(
        FirebaseMessagingService.NOTIFICATION_SERVICE
    ) as NotificationManager

    val notificationStyle = manager.activeNotifications.lastOrNull {
        it.id == notificationId
    }?.let {
        MessagingStyle.extractMessagingStyleFromNotification(it.notification)
    } ?: MessagingStyle(Person.Builder().setName("Me").build())

然后使用此样式构建您的通知(记住添加您的最新消息)。

val notification = NotificationCompat.Builder(context, channelId)
        .setStyle(
            notificationStyle.addMessage(
                <Latest message goes here>
            )
        )
© www.soinside.com 2019 - 2024. All rights reserved.