android,如何检查或查找活动通知匹配自定义数据

问题描述

android 应用,一个模块从远程接收不同类型的数据,并将数据传递给第 3 方通知库以构建数据特定通知并将其发布到通知抽屉。

但是,如果稍后它接收到新数据并且如果该数据类型在通知抽屉中仍然有活动通知(用户尚未点击它),则模块需要检查并找到该类型的活动通知,并将通知标签、id 和数据传递给通知库以更新现有的活动通知。

在接收远程数据的模块中,可以获得所有的活动通知

var foundAndReplaced = false
val activeSbn: StatusBarNotification[] = NotificationManager.getActiveNotifications()
for for (i in activeSbn.indices)  {
    val id = activeSbn[i].getId()
    val tag = activeSbn[i].getTag()
    val notification: Notification = activeSbn[i].getNotification()

    val thePendingIntent: PendingIntent = notification.contentIntent()

    ///
    // check if thePendingIntent was build with same data type,how???
    // foundAndReplaced = true
    if (foundAndReplaced) {
        theNotificationHandler.repostNotification(tag,id,data)
        break
    }
    ///

}
if (!foundAndReplaced) {
   theNotificationHandler.postNotification(data) // build and post a new notification for that data type
} 

但是如何检查 Notification 是否是具有特定数据类型的通知?

这个想法是,例如,接收到数据 {dataType: A,latestData; B}。

然后从 PendingIntent 中取出 Intent,并检查 Intent,如下所示:

/// ??? not sure how to do this,it does not have the getIntent()
var oldIntent = thePendingIntent.getIntent()
foundAndReplaced =  (oldIntent.getString(DATA_TYPE_KEY == "A") // this active notification is for the dataTYpe == A
   

并且当更新的通知被点击时,intent 将被传递给 Intent 中配置的带有新数据“B”的 Activity。

有没有办法检查通知的 pendingIntent/Intent 中的数据?或者如何根据某些数据检查通知?

编辑:总结想要做什么

  1. 当收到来自 FCM 消息的推送通知时,通知会显示并保存在通知抽屉中(直到用户点击它)。一个 Intent(带有客户数据)被放入 PendingIntent 以对用户的点击做出反应(打开一个 Activity 并将 Intent 传递给它等)
received custom data: {dataType: A,latestData; Dat-A}
and put in the Intent: 
val customData = "{dataType: A,latestData; Dat-A}"
val intent = Intent(this,PushHandlerActivity::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    putExtra(CUSTOM_MESSAGE_KEY,customData)

}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this,intent,0)
...
and then build the notification and post it:
val builder = NotificationCompat.Builder(this,CHANNEL_ID)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)
        
with(NotificationManagerCompat.from(this)) {
    notificationManager.notify(notificationId,builder.build())
}

  1. 如果收到与之前收到的推送具有相同自定义数据类型的新推送,并且如果之前发布的通知在通知抽屉中仍处于活动状态(用户尚未点击它),则不应有新通知被构造并发布到通知抽屉,但它应该找出之前发布的通知(未使用且在通知抽屉中仍处于活动状态),并更新放入给 PendingtIntent 的意图中的自定义数据。

示例。第一次得到推送数据:{dataType: A,latestData; Dat-A}

稍后它会收到另一个带有数据的推送

{dataType: A,latestData; Dat-B}

现在需要做的是检查活动通知(通过NotificationManager.getActiveNotifications()),以查找是否有自定义数据类型==“A”,如果找到则更新该活动通知的意图的 cutomData(被放入额外的)到 {dataType: A,latestData; Dat-B}

问题出在活动通知(StatusBarNotification)上,它可以获得pendingIntent:

val thePendingIntent: PendingIntent = aStatusBarNotification.contentIntent()

但是由于原始意图被放在 pendingIntent 中(通过 PendingIntent.getActivity(this,0)),所以需要先从 pendingIntent 中取出意图。但似乎不能。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)