Android RemoteViews SetOnClickPendingEvent即使在不同的通知中也会覆盖相同视图ID的意图
我的应用程序需要显示多个通知。每个通知都使用相同的自定义布局xml,该布局xml有一个按钮,该按钮使用额外的int(数字X)触发一个意图,该int对于每个通知都是唯一的 这是由RemoteView完成的,因此我对每个通知都执行类似的操作:Android RemoteViews SetOnClickPendingEvent即使在不同的通知中也会覆盖相同视图ID的意图,android,android-notifications,Android,Android Notifications,我的应用程序需要显示多个通知。每个通知都使用相同的自定义布局xml,该布局xml有一个按钮,该按钮使用额外的int(数字X)触发一个意图,该int对于每个通知都是唯一的 这是由RemoteView完成的,因此我对每个通知都执行类似的操作: RemoteViews remoteViews = new RemoteViews(...); remoteViews.setOnClickPendingIntent(R.id.myButton, myIntentWithUniqueId) Notificat
RemoteViews remoteViews = new RemoteViews(...);
remoteViews.setOnClickPendingIntent(R.id.myButton, myIntentWithUniqueId)
Notification myNotification = new NotificationCompat.Builder(context)
.setContent(remoteViews)
...
.build();
我在显示通知时使用不同的id
notificationManager.notify(someUniqueId, myNotification)
我发现,即使我可以显示多个通知,所有通知中的所有按钮都与上次显示的通知具有相同的意图。当按下每个通知中的按钮时,可以通过查看每个通知中的意图包含相同的额外int X来验证
这是一个bug还是出于设计?您的问题可能会解决这个问题。将悬挂式帐篷添加到TaskStackBuilder。 这将检索与特定通知关联的挂起内容。例如
android.support.v4.app.TaskStackBuilder stackBuilder = android.support.v4.app.TaskStackBuilder.create(this);
// TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(notificationid, PendingIntent.FLAG_UPDATE_CURRENT);
//resultPendingIntent = PendingIntent.getActivity(this, notificationid, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
或者尝试使用广播意图,比如
Intent resultDismissintent = new Intent(getApplicationContext(), NotificationCancelReceiver.class);
resultDismissintent.putExtra("notificationId", notificationid);
resultDismissintent.putExtras(resultIntent.getExtras());
PendingIntent resultCancelPendingIntent = PendingIntent.getBroadcast(this, notificationid, resultDismissintent, PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.tv_view, resultCancelPendingIntent);
contentView.setOnClickPendingIntent(R.id.tv_cancel, resultPendingIntent);
我不太明白如何使用TaskStackBuilder解决这个问题。在我看来,您正在调用NotificationBuilder#setContentIntent,但正如您所看到的,我的意图实际上是在RemoteView中为特定视图设置的(我实际上在RemoteView中有三个按钮,但我认为用一个按钮提问更容易),因此我没有为通知设置意图。