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

如何解决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 中取出意图。但似乎不能。

解决方法

您无法从 Intent 中取出 PendingIntent

对于不同的数据类型,您可以在 Intent 中使用不同的 ACTION,因此当您获得特定数据类型的新数据时,您可以更新该数据类型的任何现有 PendingIntent 中的数据.只需使用 PendingIntent.FLAG_UPDATE_CURRENT 更新现有 PendingIntent 中的数据。

注意:我假设您在 Intent 中的“数据”存储为“额外”,而不是存储在 URI 字段中。


这是一个例子:

val customData = "{dataType: A,latestData; Dat-A}"
val customAction = "DataTypeA" // different string for each data type
                               //  ensures a unique PendingIntent for
                               //  each data type
val intent = Intent(this,PushHandlerActivity::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or
            Intent.FLAG_ACTIVITY_CLEAR_TASK
    action = customAction
    putExtra(CUSTOM_MESSAGE_KEY,customData)
}
// Using FLAG_UPDATE_CURRENT just updates the "extras" on an existing
//  PendingIntent
val pendingIntent: PendingIntent = PendingIntent.getActivity(this,intent,PendingIntent.FLAG_UPDATE_CURRENT)

此代码将为每种不同的数据类型创建不同的 PendingIntent。但是如果特定数据类型的 PendingIntent 已经存在,这将简单地更新现有 PendingIntent 中的“额外”。如果 Notification 已经存在,您甚至不必再次发布它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?