我在 GCMIntentservice 中编写了一段代码,用于向许多用户发送推送通知。我使用的NotificationManager将在单击通知时调用DescriptionActivity类。我还将 GCMIntentService 中的 event_id 发送到 DescriptionActivity
protected void onMessage(Context ctx, Intent intent) {
message = intent.getStringExtra("message");
String tempmsg=message;
if(message.contains("You"))
{
String temparray[]=tempmsg.split("=");
event_id=temparray[1];
}
nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(this, DescriptionActivity.class);
Log.i("the event id in the service is",event_id+"");
intent.putExtra("event_id", event_id);
intent.putExtra("gcmevent",true);
PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0);
String title="Event Notifier";
Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis());
n.setLatestEventInfo(this, title, message, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueID,n);
sendGCMIntent(ctx, message);
}
这里我在上述方法中获得的 event_id 是正确的,即我总是得到更新的。但在下面的代码(DescriptionActivity.java)中:
intent = getIntent();
final Bundle b = intent.getExtras();
event_id = Integer.parseInt(b.getString("event_id"));
此处的 event_id 始终为“5”。无论我在 GCMIntentService 类中放置什么,我得到的 event_id 始终是 5。有人可以指出问题吗?是因为未决意图吗?如果是的话我该如何处理?
最佳答案
PendingIntent
与您提供的第一个 Intent
一起重用,这是您的问题。
为避免这种情况,请使用标志 PendingIntent.FLAG_CANCEL_CURRENT
当您调用 PendingIntent.getActivity()
来实际获取一个新的事件时:
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
或者,如果您只想更新附加内容,请使用标志 PendingIntent.FLAG_UPDATE_CURRENT
关于push-notification - putExtra 使用挂起意图不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376643/