【问题标题】:Retrieve message from a notification push从通知推送中检索消息
【发布时间】:2022-01-20 06:28:09
【问题描述】:

此推送通知用于通知何时有新的聊天消息可用。我想像 Instagram 一样,也就是说,每次收到新的收入消息时都会更新此通知中的消息,直到用户打开聊天。

那么,我如何检索此特定通知的消息?是否有可能,或者我必须保存(在缓存中)最新消息...?

非常感谢

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    我建议将消息​​保存在数据库中,然后在发送通知的函数中获取消息(可能是最近未读的消息——我不知道在这种情况下你的逻辑是什么样的)。 如果有多个,使用 InboxStyle():

    val otherMessagesToShow = getMostRecentUnreadMessages()
                if(otherMessagesToShow.size > 1) {
                    val style = NotificationCompat.InboxStyle()
                    otherMessagesToShow.forEach { style.addLine(it) }
                    builder.setStyle(style)
                    builder.setNumber(otherMessagesToShow.size)
                }
    

    最后,更新现有的(传递相同的notificationID):

    notificationManager.notify(notificationId, builder.build())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      相关资源
      最近更新 更多