我同时与多发通知问题putExtra
使用PendingIntent
。在Activity
getStringExtra
fomIntent
中putExtra
生成时最后返回Notification
。
首先让我解释完整的情况,如果我错了,请让我正确。
首先,我Activity
的(就是它的MainActivity
)launchMode
是singleTop
。我将其设置为manifest.ie
<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++;
我也在使用flag 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 for GCM发送时,Intent Extras丢失了
请帮忙。
您不是在创建唯一PendingIntent
的。在Stackoverflow上,对于这个问题必须有一千个答案。
当您致电:
PendingIntent pendingIntent = PendingIntent.getActivity(
BeaconService.this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
第二次,它将找到第一个,PendingIntent
并向您返回对其的引用(Android不会创建另一个)。此外,由于您进行了设置FLAG_UPDATE_CURRENT
,因此Android会Intent
通过用Intent
新的中提供的附加功能覆盖旧的附加功能来修改基础Intent
。这就是为什么它总是只交付一个Intent
,并且Intent
总是拥有最后一组附加功能的原因。
您需要使每个PendingIntent
唯一。有许多方法可以做到这一点(设置其他功能不是其中一种方法)。尝试提供一个唯一的数字作为的requestCode
参数PendingIntent.getActivity()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句