3

I have implemented a simple GCM client and I am having this problem when updating the Activity which launches each time a new notification is received. Every time I send a notification with a set of parameters, only the parameters I sent for the first notification appears in the UI. When I try changing the parameters, they don't show up (the same data is shown 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 which launches on click of the notification.

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 got something to do with the PendingIntent but I'm not sure what is going on here since I'm relatively new to using PendingIntents and notifications in Android. Can anyone please tell me what is happening?

shyam
  • 1,348
  • 4
  • 19
  • 37

1 Answers1

5

In sendNotification you get the PendingIntent using this code:

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

The first time you do this, Android will create a new PendingIntent using the Intent that you provide as the 3rd parameter of this call. The following times you do this, Android will not create a new PendingIntent, but will return a token that refers to the first PendingIntent that you created.

If you want Android to replace the "extras" on the original PendingIntent when you execure this code the next time, you need to use PendingIntent.FLAG_UPDATE_CURRENT as the 4th parameter in the call to getActivity(). This will still return a token that referes to the first PendingIntent that you created, but it will also replace all the "extras" in that original PendingIntent with the "extras" present in the Intent passed as the 3rd parameter.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks this worked for me. @DavidWasser please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:09