gpt4 book ai didi

android - 第二个 Action 的 PendingIntent 会覆盖第一个 Action 和通知的 contentIntent

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:54 30 4
gpt4 key购买 nike

代码:

int id = 0;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.notification_on_the_move_gps_title))
.setContentText(context.getString(R.string.notification_on_the_move_text));

builder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.notification_on_the_move_gps_big_text)));

Intent mainIntent = new Intent(context, MainActivity.class);

Intent turnOffIntent = new Intent(context, MainActivity.class);
turnOffIntent.putExtra(MainApp.KEY_TURN_OFF_NOTIFICATION_ID, id);

TaskStackBuilder mainBuilder = TaskStackBuilder.create(context);
mainBuilder.addParentStack(MainActivity.class);
mainBuilder.addNextIntent(mainIntent);
PendingIntent mainPendingIntent = mainBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(mainPendingIntent);

TaskStackBuilder turnOffBuilder = TaskStackBuilder.create(context);
turnOffBuilder.addParentStack(MainActivity.class);
turnOffBuilder.addNextIntent(turnOffIntent);
PendingIntent turnOffPendingIntent = turnOffBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

builder
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setLights(Color.BLUE, 500, 500)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.addAction(R.drawable.ic_stat_notification, "Open", mainPendingIntent)
.addAction(R.drawable.ic_stat_notification_off, "Turn off", turnOffPendingIntent);

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(id, builder.build());

问题:

点击未展开或展开的通知正文或“打开”操作按钮总是会传递额外的 (KEY_TURN_OFF_NOTIFICATION_ID) Intent 。

我试过放弃 TaskStackBuilder 并简单地创建 PendingIntent:

Intent mainIntent = new Intent(context, MainActivity.class);
PendingIntent piMain = PendingIntent.getActivity(context, 0, mainIntent, 0);

Intent turnOffIntent = new Intent(context, MainActivity.class);
turnOffIntent.putExtra(MainApp.KEY_TURN_OFF_NOTIFICATION_ID, id);
PendingIntent piTurnOff = PendingIntent.getActivity(context, 0, turnOffIntent, 0);

但是,同样的效果:(

如果我省略 setContentIntent(),自然地,点击通知正文不会执行任何操作。

如果我设置 setContentIntent()设置操作之后,那么 MainActivity 永远不会收到额外的内容。

最佳答案

来自doc :

Because of this behavior, it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent.filterEquals. If you use two Intent objects that are equivalent as per Intent.filterEquals, then you will get the same PendingIntent for both of them.

因此您必须使这些 Intent 与 Intent.filterEquals 方法中的不同:

That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

作为旁注,我在处理 firefox for android 的下载通知时遇到了同样的问题 here

关于android - 第二个 Action 的 PendingIntent 会覆盖第一个 Action 和通知的 contentIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652895/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com