Notifications android,它能在不保存用于生成通知的生成器的情况下更新活动通知吗
android应用程序,当获取新数据并需要向通知抽屉发布通知时,如果以前的通知仍在活动通知列表中(用户未点击或刷卡以取消通知),则需要更新或替换旧通知 这里的问题是以前的通知是从第三方sdk构建的,并发布到通知抽屉中,这里我们只能从Notifications android,它能在不保存用于生成通知的生成器的情况下更新活动通知吗,notifications,android-notifications,Notifications,Android Notifications,android应用程序,当获取新数据并需要向通知抽屉发布通知时,如果以前的通知仍在活动通知列表中(用户未点击或刷卡以取消通知),则需要更新或替换旧通知 这里的问题是以前的通知是从第三方sdk构建的,并发布到通知抽屉中,这里我们只能从 statusBarNotificationList = ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications())
statusBarNotificationList = ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).getActiveNotifications())
并从返回的活动状态通知列表中查找特殊密钥
val notificationToReplace: Notification? = null
for (int i = statusBarNotificationList.size() - 1; (i > 0); i--){
notificationToReplace = If( statusBarNotificationList[i].getNotification().extras.get("theSpecialKey") == "KEY_UPDATE_BY_REPLACE") else null
if (notificationToReplace != null) break
}
// how to update the notificationToReplace here???
在google android通知示例中,它评论:
* IMPORTANT NOTE 1: You shouldn't save/modify the resulting Notification object using
* its member variables and/or legacy APIs. If you want to retain anything from update
* to update, retain the Builder
在此用例中,它将需要更新此现有活动通知的额外
,不能只执行以下操作:
notificationToReplace.extras = newExtra
notificationMgr.notif(notificationToReplace.getId(), notificationToReplace)
此处不能有原始的Notification.Builder
有什么建议吗
编辑:
似乎无法从PendingEvent中获取自定义数据或进行更新
val intent = Intent(this, AlertDetails::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, 0, intent, 0)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(this)) {
notificationManager.notify(notificationId, builder.build())
}
这通常是通过notify()生成和发布通知的方式
自定义数据放在意图的额外部分,意图放在Pending帐篷中
val intent = Intent(this, AlertDetails::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, 0, intent, 0)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(this)) {
notificationManager.notify(notificationId, builder.build())
}
它是放入待传递到活动的PendingEvent中的意图
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
val customData: CustomeData = intent.getParcelableExtra<CustomeData>(CUSTOM_MESSAGE_KEY)
}
覆盖Wintent(意图:意图){
super.onNewIntent(意图)
val customData:CustomeData=intent.getParcelableExtra(自定义消息密钥)
}
因此,如果您希望重用活动通知,但只替换其旧的自定义数据,则必须符合PendingEvent中的意图
val intent = Intent(this, AlertDetails::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, 0, intent, 0)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(this)) {
notificationManager.notify(notificationId, builder.build())
}
它有没有一种方法可以把意图从悬垂的帐篷中释放出来