我有一個完美的GCM通知實現。但是,問題是一旦接收到的方法意圖收到消息,則顯示的消息始終是舊消息。 'extras.getString(「payload」)'總是顯示舊信息。我似乎無法弄清楚問題所在。GCM android推送通知總是顯示舊消息。意圖收到不正確
發送所述GCM通知的類是:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class C2DMMessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("C2DM", "Received message");
String payload = intent.getStringExtra("payload");
Log.d("C2DM", "dmControl: payload = " + payload);
// TODO Send this to my application server to get the real data
// Lets make something visible to show that we received the message
createNotification(context, payload);
}
}
public void createNotification(Context context, String payload) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"Message sent!", System.currentTimeMillis());
// Hide the notification after its selected
//notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
Intent intent = new Intent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("NotifID", 1);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
notification.setLatestEventInfo(context, "Message","Message Recieved", pendingIntent);
notificationManager.notify(0, notification);
}
}
其中臨危通知消息的類是:
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.TextView;
public class MessageReceivedActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_result);
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
//---cancel the notification---
int id=getIntent().getExtras().getInt("NotifID");
notificationManager.cancelAll();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String message = extras.getString("payload");
if (message.equals("call")) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:9916261960"));
startActivity(intent);
} else if (message.equals("camera")) {
Intent cameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(cameraIntent);
} else {
if (message != null && message.length() > 0) {
TextView view = (TextView) findViewById(R.id.result);
view.setText(message);
}
}
}
super.onCreate(savedInstanceState);
}
}
這裏,額外.getString( 「有效載荷」);始終保持第一次發送通知消息。
工程。謝謝。 – jasdmystery
我有同樣的問題。但「FLAG_UPDATE_CURRENT」不適合我。我仍然收到舊信息。 – viji
適合我,謝謝 – TheMan