0

I have the following code:

public void sendNewNotification (Booking updateBooking) {

    String msg;
    if (updateBooking.getJobStatus () == Booking.eeJob_OnRoute) {
        msg = "Your driver has been dispatched.";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_POB) {
        msg = "Your driver has arrived.";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_Complete) {
        Variables.driverrated = false;
        msg = "Your booking has been completed. Thank you for using " + Variables.setting.getCompanysName () + ".";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_NoShow) {
        msg = "Unfortunately your driver was unable locate you. Please call the office on " + Variables.setting.getCompanytelephone () + ".";
    } else if (updateBooking.getJobStatus () == Booking.eeJob_Cancelled) {
        msg = "Your booking has been cancelled. Please call the office on " + Variables.setting.getCompanytelephone () + " if this is incorrect.";
    } else {
        return;
    }

    PendingIntent detailsIntent =  PendingIntent.getActivity (this, 0, new Intent (this, HomeActivity.class)
            .putExtra ("booking", updateBooking)
            .putExtra ("track", false),
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent trackingIntent = PendingIntent.getActivity  (this, 0, new Intent (this, HomeActivity.class)
            .putExtra ("booking", updateBooking)
            .putExtra ("track", true),
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder (this);
    builder.setAutoCancel (true);
    builder.setDefaults (Notification.DEFAULT_ALL);
    builder.setWhen (System.currentTimeMillis ());
    builder.setContentTitle (Variables.setting.getCompanysName () + " - booking update");
    builder.setSmallIcon (R.drawable.notification);
    builder.setTicker (Variables.setting.getCompanysName () + " - booking update");
    builder.setPriority (NotificationCompat.PRIORITY_HIGH);
    builder.setContentText (msg);
    builder.setContentIntent (trackingIntent);

    builder.addAction (R.drawable.documents_32px, "DETAILS", detailsIntent);

    if (updateBooking.getJobStatus () != Booking.eeJob_Complete) {
        builder.addAction (R.drawable.notification, "TRACK", trackingIntent);
    } else {
        builder.addAction (R.drawable.profile_32px, "RATE", detailsIntent);
    }

    NotificationManager manager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);
    manager.notify (getTaskId (), builder.build ());

}

My issue is in my onCreate function I have these two lines:

m_updateBooking = (Booking) getIntent ().getSerializableExtra ("booking");
m_TrackDriver = (Boolean) getIntent ().getSerializableExtra ("track");

m_updateBooking works flawlessly. But m_TrackDriver is ALWAYS true. No matter what button I press. Can anyone explain why this is?

Thanks in advance for all your help.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
apmartin1991
  • 3,064
  • 1
  • 23
  • 44

1 Answers1

3

Since your Intent has the same action for both the PendingIntent, you need to specify a different requestCode otherwise with FLAG_UPDATE_CURRENT the extras in the PendingIntent will be replaced by the new extras in the Intent. For this reason you have always true, because it is in the last PendingIntent requested.

This StackOveflow answer explains in details how PendingIntent works.

To resolve your issue change your code like this:

PendingIntent detailsIntent =  PendingIntent.getActivity (this, 0, new Intent (this, HomeActivity.class)
        .putExtra ("booking", updateBooking)
        .putExtra ("track", false),
        PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent trackingIntent = PendingIntent.getActivity  (this, 1, new Intent (this, HomeActivity.class)
        .putExtra ("booking", updateBooking)
        .putExtra ("track", true),
        PendingIntent.FLAG_UPDATE_CURRENT);
Community
  • 1
  • 1
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94