La Notificación de empuje de Android de GCM muestra siempre el mensaje antiguo. Intención recibida incorrecta

Tengo una implementación de notificación de GCM que funciona perfectamente. Pero, los problemas es una vez que el mensaje ha sido recibido en la intención en el método recibido, el mensaje mostrado siempre es el viejo mensaje. Esa es la 'extras.getString ("payload")' siempre muestra el mensaje antiguo. No puedo entender cuál es el problema.

La clase que envía la notificación GCM es:

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); } 

}

La clase que recibe el mensaje de notificación es:

 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); } 

}

Aquí, los extras.getString ("payload"); Mantiene el mensaje de notificación enviado por primera vez siempre.

Al crear la intención pendiente, utilice FLAG_UPDATE_CURRENT

 PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

De lo contrario, la intención antigua se reutiliza sin los nuevos extras

Trate de buscar su intención dentro

@Override protected void onNewIntent (intención de intención) {

 super.onNewIntent(intent); Bundle extras = intent.getExtras(); fromScreen = getIntent().getIntExtra("FROMSCREEN", Config.SHARE_SCREEN_TAG); //enter code here 

}

  • Mantener el ID de registro de la aplicación GCM válido durante las actualizaciones automáticas
  • GCM / FCM: no recibe eventos, devolución de llamada de intención de difusión: resultado = CANCELADO
  • Anulación de registro de GCM causando que la aplicación se bloquee
  • GCM - Rara vez recibiendo mensajes temáticos
  • Mensajería de Google Cloud utilizando el equipo
  • ¿Cómo puedo recibir notificaciones múltiples usando GCM sin reemplazar la precedente?
  • ¿Es posible utilizar GCM sin la cuenta de Google y Google Play Services en el dispositivo?
  • ¿Está com.google.android.c2dm.intent.RECEIVE todavía en uso?
  • Java.io.IOException: SERVICE_NOT_AVAILABLE en el cliente de GCM
  • Cómo usar Google Cloud Connection Server para enviar mensajes de GCM en sentido ascendente
  • ¿Cómo los servidores de la conexión GCM envían mensajes a un dispositivo Android?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.