【发布时间】:2015-03-19 09:48:24
【问题描述】:
当putExtra 使用PendingIntent 时,我遇到了多个通知问题。在Activity getStringExtra 中,Intent 正在返回最后一个putExtra,同时生成Notification。
让我先解释完整的场景,如果我错了,请纠正我。
首先是我的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");
}
}
返回的附加信息总是来自上次通知的附加信息。不知道哪里出错了?
我也尝试了一些链接,但没有找到有用的解决方案。
Intent extras being lost when sending with PendingIntent for GCM
请帮忙。
【问题讨论】:
标签: android notifications android-pendingintent