覆盖意图额外
Override intent extras
我正在使用以下代码 (Kotlin) 创建通知
val builder = NotificationCompat.Builder(ctx)
........
.setContentIntent(PendingIntent.getActivity(ctx, 891, ctx.newIntent<MainActivity>()
.putExtra("id", member.id)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0))
因此,当通知被点击时,MainActivity
将 select 用户,通知来自该用户。
override fun onNewIntent(intent: Intent?) {
val id = intent?.getStringExtra("id") ?: return
selectUser(extra)
}
我从 2 个不同的用户发送了 2 个通知。单击第一个通知后,它可以正常工作 (id==_User1UUID) 和 selects 用户。然后我按回键,从第二个用户发送另一个通知,点击它并且意图仍然包含以前的用户 ID 和 selects 它(通过断点检查)。
我知道,这是因为 FLAG_ACTIVITY_REORDER_TO_FRONT
,但我必须只保留一个 MainActivity
。
您实际上需要给定的代码才能使每个通知都独一无二
notificationManager.notify( (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE) /* ID of notification */, notificationBuilder.build());
如果您已经这样做了,请尝试下面给出的代码
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
您可能遇到的问题是您没有生成唯一的 PendingIntent
。如果您有 2 个针对不同用户的通知,他们都将使用相同的 PendingIntent
,因此您会在两者中看到相同的 id
。
要创建唯一的 PendingIntent
s,请更改:
.setContentIntent(PendingIntent.getActivity(ctx, 891, ctx.newIntent<MainActivity>()
.putExtra("id", member.id)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0))
对此:
int randomNumber = ... // Use some random number here, or use your "id" if your "id" can be converted to an integer here.
// This random number needs to be unique for each Notification you create.
.setContentIntent(PendingIntent.getActivity(ctx, randomNumber, ctx.newIntent<MainActivity>()
.putExtra("id", member.id)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), u))
我正在使用以下代码 (Kotlin) 创建通知
val builder = NotificationCompat.Builder(ctx)
........
.setContentIntent(PendingIntent.getActivity(ctx, 891, ctx.newIntent<MainActivity>()
.putExtra("id", member.id)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0))
因此,当通知被点击时,MainActivity
将 select 用户,通知来自该用户。
override fun onNewIntent(intent: Intent?) {
val id = intent?.getStringExtra("id") ?: return
selectUser(extra)
}
我从 2 个不同的用户发送了 2 个通知。单击第一个通知后,它可以正常工作 (id==_User1UUID) 和 selects 用户。然后我按回键,从第二个用户发送另一个通知,点击它并且意图仍然包含以前的用户 ID 和 selects 它(通过断点检查)。
我知道,这是因为 FLAG_ACTIVITY_REORDER_TO_FRONT
,但我必须只保留一个 MainActivity
。
您实际上需要给定的代码才能使每个通知都独一无二
notificationManager.notify( (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE) /* ID of notification */, notificationBuilder.build());
如果您已经这样做了,请尝试下面给出的代码
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
您可能遇到的问题是您没有生成唯一的 PendingIntent
。如果您有 2 个针对不同用户的通知,他们都将使用相同的 PendingIntent
,因此您会在两者中看到相同的 id
。
要创建唯一的 PendingIntent
s,请更改:
.setContentIntent(PendingIntent.getActivity(ctx, 891, ctx.newIntent<MainActivity>()
.putExtra("id", member.id)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0))
对此:
int randomNumber = ... // Use some random number here, or use your "id" if your "id" can be converted to an integer here.
// This random number needs to be unique for each Notification you create.
.setContentIntent(PendingIntent.getActivity(ctx, randomNumber, ctx.newIntent<MainActivity>()
.putExtra("id", member.id)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), u))