从 notificationIntent in activity while launchMode="singleTop" 获取额外的多条通知
Get extra from notificationIntent in activity while launchMode="singleTop" with multiple notifications
我在 putExtra
使用 PendingIntent
时遇到多个通知的问题。在 Activity
getStringExtra
中,fom Intent
在生成 Notification
.
时最后返回 putExtra
让我先解释完整的场景,如果我错了,请让我更正。
首先我的 Activity
(假设它的 MainActivity
)launchMode
是 singleTop
。我将其设置为 manifest.i.e.
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
... />
现在我正在使用此代码生成通知,
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context,
MainActivity.class);
notifyIntent.putExtra("message", msg);
Log.i(TAG, msg);
notifyIntent.putExtra("title", notification_title);
Log.i(TAG, notification_title);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(notification_title).setContentText(msg)
.setAutoCancel(true).setContentIntent(pendingIntent).build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
NOTIFICATION_ID++;
我也在使用标志 PendingIntent.FLAG_UPDATE_CURRENT
。但是,while clicking on any Notification (let's say we've 5 notifactions) it just returns the fifth extra put while generating Notification all top four notification extra are just like lost somewhere
.
在我的 MainActivity
中,我还重写了 onNewIntent
,例如,
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
if (intent.hasExtra("message")) {
String message = intent.getStringExtra("message");
}
if (intent.hasExtra("title")) {
String title = intent.getStringExtra("title");
}
}
返回的额外内容总是来自上次通知的额外内容。我不知道我哪里错了?
我也尝试了一些链接,但没有找到有用的解决方案。
请帮忙。
您没有创建唯一的 PendingIntent
。这个问题在 Whosebug 上肯定有一千个答案。
当你这样调用时:
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
第二次,它会找到第一个 PendingIntent
和 return 你对它的引用(Android 不会创建另一个)。此外,由于您设置了 FLAG_UPDATE_CURRENT
,Android 将修改基础 Intent
,方法是用您在新 Intent
中提供的额外内容覆盖旧 Intent
中的额外内容.这就是为什么它总是只提供一个 Intent
而 Intent
总是有最后一组额外的。
您需要让每个 PendingIntent
都独一无二。有很多方法可以做到这一点(设置不同的附加功能不是其中一种方法)。尝试向 PendingIntent.getActivity()
.
提供一个唯一编号作为 requestCode
参数
@David Wasser 感谢您的回答!
我只是想分享一段我用过并解决了我的问题的代码。
将唯一编号作为 requestCode
参数提供给 PendingIntent.getActivity()
是多重通知的正确解决方案,而 launchmode="singleTop"
.
To provide unique
代码作为 requestCode
我所做的是,将 NOTIFICATION_ID
放置为 requestCode
即
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, NOTIFICATION_ID, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
我也找到了相同的 solution at here。
那就是 perfect unique requestCode in my case
。希望对遇到问题的其他人有所帮助。
我在 putExtra
使用 PendingIntent
时遇到多个通知的问题。在 Activity
getStringExtra
中,fom Intent
在生成 Notification
.
putExtra
让我先解释完整的场景,如果我错了,请让我更正。
首先我的 Activity
(假设它的 MainActivity
)launchMode
是 singleTop
。我将其设置为 manifest.i.e.
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
... />
现在我正在使用此代码生成通知,
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context,
MainActivity.class);
notifyIntent.putExtra("message", msg);
Log.i(TAG, msg);
notifyIntent.putExtra("title", notification_title);
Log.i(TAG, notification_title);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(notification_title).setContentText(msg)
.setAutoCancel(true).setContentIntent(pendingIntent).build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
NOTIFICATION_ID++;
我也在使用标志 PendingIntent.FLAG_UPDATE_CURRENT
。但是,while clicking on any Notification (let's say we've 5 notifactions) it just returns the fifth extra put while generating Notification all top four notification extra are just like lost somewhere
.
在我的 MainActivity
中,我还重写了 onNewIntent
,例如,
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
if (intent.hasExtra("message")) {
String message = intent.getStringExtra("message");
}
if (intent.hasExtra("title")) {
String title = intent.getStringExtra("title");
}
}
返回的额外内容总是来自上次通知的额外内容。我不知道我哪里错了?
我也尝试了一些链接,但没有找到有用的解决方案。
请帮忙。
您没有创建唯一的 PendingIntent
。这个问题在 Whosebug 上肯定有一千个答案。
当你这样调用时:
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
第二次,它会找到第一个 PendingIntent
和 return 你对它的引用(Android 不会创建另一个)。此外,由于您设置了 FLAG_UPDATE_CURRENT
,Android 将修改基础 Intent
,方法是用您在新 Intent
中提供的额外内容覆盖旧 Intent
中的额外内容.这就是为什么它总是只提供一个 Intent
而 Intent
总是有最后一组额外的。
您需要让每个 PendingIntent
都独一无二。有很多方法可以做到这一点(设置不同的附加功能不是其中一种方法)。尝试向 PendingIntent.getActivity()
.
requestCode
参数
@David Wasser 感谢您的回答!
我只是想分享一段我用过并解决了我的问题的代码。
将唯一编号作为 requestCode
参数提供给 PendingIntent.getActivity()
是多重通知的正确解决方案,而 launchmode="singleTop"
.
To provide unique
代码作为 requestCode
我所做的是,将 NOTIFICATION_ID
放置为 requestCode
即
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, NOTIFICATION_ID, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
我也找到了相同的 solution at here。
那就是 perfect unique requestCode in my case
。希望对遇到问题的其他人有所帮助。