Why is my activity always showing the same dataset from the GCM notification?

I have implemented a simple GCM client and I am having this problem when updating an Activity that starts every time a new notification is received. Every time I send a notification with a set of parameters, only the parameters that I sent for the first notification appear in the UI. When I try to change the options, they are not displayed (the same data is displayed again).

Here is my GcmIntentService class

public class GcmIntentService extends IntentService {

private static final String TAG = "GcmIntentService";

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GcmIntentService() {
    super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {

    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            Log.d(TAG, "GCM error -> MESSAGE_TYPE_SEND_ERROR");
            sendNotification("Send error: " + extras.toString(), "", "",
                    "", "");
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            Log.d(TAG, "GCM error -> MESSAGE_TYPE_DELETED");
            sendNotification(
                    "Deleted Messages on server: " + extras.toString(), "",
                    "", "", "");
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {
            Log.d(TAG,
                    "GCM success. Received data: fromUser="
                            + extras.getString("fromUser") + " message="
                            + extras.getString("message") + " srcLat="
                            + extras.getString("srcLat") +" srcLng="
                            + extras.getString("srcLng") + " dstLat="
                            + extras.getString("dstLat") + " dstLng="
                            + extras.getString("dstLng"));
            sendNotification(
                    extras.getString("fromUser") + " "
                            + extras.getString("message"),
                            extras.getString("srcLat"), extras.getString("srcLng"),
                            extras.getString("dstLat"), extras.getString("dstLng"));
        }

    }

    GcmBroadcastReceiver.completeWakefulIntent(intent);

}

private void sendNotification(String msg, String srcLat, String srcLng,
        String dstLat, String dstLng) {

    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, ShowUser.class);
    intent.putExtra("message", msg);
    intent.putExtra("srcLat", srcLat);
    intent.putExtra("srcLng", srcLng);
    intent.putExtra("dstLat", dstLat);
    intent.putExtra("dstLng", dstLng);

    Log.d(TAG, "-> sendNotification -> Received parameters:\nmessage:"
            + msg + "\nsrcLat=" + srcLat + "\nsrcLng=" + srcLng
            + "\ndstLat=" + dstLat + "\ndstLng=" + dstLng);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Taxi Request Received")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

}

      

And here is my activity that is triggered when a notification is clicked.

public class ShowUser extends Activity {

private TextView messageTextView;
private TextView showUsernameTextView;
private TextView srcLatTextView;
private TextView srcLngTextView;
private TextView dstLatTextView;
private TextView dstLngTextView;

private SharedPreferences prefs;

private String username;

private static final String TAG = "ShowUser";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_user_details);

    Intent intent = getIntent();
    String message = intent.getStringExtra("message");
    String srcLat = intent.getStringExtra("srcLat");
    String srcLng = intent.getStringExtra("srcLng");
    String dstLat = intent.getStringExtra("dstLat");
    String dstLng = intent.getStringExtra("dstLng");

    Log.d(TAG, "ReceivedExtras: message=" + message
            + "srcLat=" + srcLat + "srcLng=" + srcLng
            + "dstLat=" + dstLat + "dstLng=" + dstLng);

    prefs = getSharedPreferences(DriverLoginActivity.USER_CREDENTIALS, Context.MODE_PRIVATE);
    username = prefs.getString(DriverLoginActivity.USERNAME, null);

    if(username!= null) {
        messageTextView = (TextView) findViewById(R.id.messageTextView);
        showUsernameTextView = (TextView) findViewById(R.id.showUsernameTextView);
        srcLatTextView = (TextView) findViewById(R.id.sourceLatTextView);
        srcLngTextView = (TextView) findViewById(R.id.sourceLngTextView);
        dstLatTextView = (TextView) findViewById(R.id.destinationLatTextView);
        dstLngTextView = (TextView) findViewById(R.id.destinationLngTextView);

        showUsernameTextView.setText("Welcome " + username);
        messageTextView.setText(message);
        srcLatTextView.setText("Source Latitude: " + srcLat);
        srcLngTextView.setText("Source Longitude: " + srcLng);
        dstLatTextView.setText("Destination Latitude: " + dstLat);
        dstLngTextView.setText("Destination Longitude: " + dstLng);

    }

}

}

      

I think it probably has something to do with PendingIntent

, but I'm not sure what's going on here as I'm relatively new to using PendingIntent

and notifications in Android. Can anyone tell me what's going on?

+3


source to share


1 answer


As sendNotification

you get PendingIntent

by using this code:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        intent, 0);

      



The first time you do this, Android will create a new PendingIntent

one using the Intent

one you provide as the third parameter of this call. The next time you do this, Android will not create a new one PendingIntent

, but will return a token that references the first PendingIntent

one you created.

If you want Android to replace "extra functions" with the original PendingIntent

when you run this code next time, you need to use it PendingIntent.FLAG_UPDATE_CURRENT

as the 4th parameter when calling getActivity()

. This will still return a marker that refers to the first PendingIntent

one you created, but it will also replace all the "extras" in that original PendingIntent

with the "extras" present in Intent

, passed as the third parameter.

+5


source







All Articles