我在我的GCMIntentservice中編寫了一個代碼,它將推送通知發送給許多用戶。我使用NotificationManager,當通知被點擊時它將調用DescriptionActivity類。我還派事項標識形成GCMIntentService到DescriptionActivityputExtra使用掛起的意圖不起作用
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);
}
這裏說我得到上述方法的事項標識是正確的即我總是得到更新之一。但是在下面的代碼中(DescriptionActivity.java):
intent = getIntent();
final Bundle b = intent.getExtras();
event_id = Integer.parseInt(b.getString("event_id"));
event_id這裏始終是「5」。不管我在GCMIntentService類中放置了什麼,我得到的event_id總是5.有人可以指出這個問題嗎?是因爲未決的意圖?如果是的話,那我該如何處理呢?
謝謝很多人!它的工作:) – Nemin 2013-05-04 17:52:54
真棒的答案!謝謝。 – 2015-12-27 08:24:19
如果它不清楚:PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT); – Alecs 2016-01-29 12:10:59