Dynamic Activity content issue

206 views Asked by At

I created a dynamic list Activity that gets its content from a PHP file (via HTTP).

So I do something like this:

public class SectionFactory {

    public static Intent getAppleList(Context ctx) {
        Intent intent = new Intent(ctx ,MainEntryListActivity.class);
        intent.putExtra("phpFileName","getApple.php");
        intent.putExtra("jsonArrayName","apples");
        intent.putExtra("pageTitle","Apples");
        return intent;
    }

    public static Intent getOrangeList(Context ctx) {
        Intent intent = new Intent(ctx ,MainEntryListActivity.class);
        intent.putExtra("phpFileName","getOrange.php");
        intent.putExtra("jsonArrayName","oranges");
        intent.putExtra("pageTitle","Oranges");
        return intent;
    }
}

This class is used from 2 parts of the app: MainActivity and GCMIntentService. Needless to say, MainActivity is the main activity of the app. It contains 2 buttons, one for apples and one for oranges. Everything here works pretty fine.

The problem is in the GCMIntentService. It is a class to handle push notifications using GCM. The idea is that I get a JSON response from the push and I decide if the notification sends me to the apple or orange list based on a value in the JSON.

The problem is, it always redirects to apples.

public class GcmIntentService extends IntentService {

public static final String TAG = GcmIntentService.class.getSimpleName();

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)) {

        } else if (GoogleCloudMessaging. MESSAGE_TYPE_DELETED.equals(messageType)) {

        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            sendNotification(extras.getString("message"));
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg) {
    try {
        Log.i(TAG,"Message: " + msg);
        JSONObject obj = new JSONObject(msg);
        String message = obj.getString("content");
        String category = obj.getString("category");

        int icon = -1;
        int notificationId = -1;
        Intent intent = null;

        if(category.equals("apples")) {
            icon = R.drawable.apples;
            intent = SectionFactory.getApples(this);
            notificationId = 1;
        }
        else if(category.equals("oranges")) {
            icon = R.drawable.oranges;
            intent = SectionFactory.getOranges(this);
            notificationId = 2;
        }

        Log.d(TAG,"Category: " + category);
        Log.d(TAG,"notificationId: " + notificationId);
        Log.d(TAG,"PHP filename: " + intent.getStringExtra("phpFileName"));

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

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

         NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContentTitle("Fruits")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message);

         mBuilder.setContentIntent(contentIntent);
         Notification notif = mBuilder.build();

         notif.defaults |= Notification.DEFAULT_SOUND;
         notif.defaults |= Notification.DEFAULT_VIBRATE;

         //notif.flags |= Notification.DEFAULT_LIGHTS;
         notif.flags |= Notification.FLAG_AUTO_CANCEL;

         mNotificationManager.notify(notificationId, notif);
    }
    catch(Exception e) { e.printStackTrace(); }
}

}

I also cheched this logs

Log.d(TAG,"Category: " + category);
Log.d(TAG,"notificationId: " + notificationId);
Log.d(TAG,"PHP filename: " + intent.getStringExtra("phpFileName"));

And they print out oranges information, but the page is created with apple information.

Why, is there any type of cache or something that I am missing?

EDIT: there are about 5 fruits, not only apples and oranges. But it always shows apple info.

EDIT 2: GCMIntentService also can open a non-dynamic activity if(category.equals("home")) and it works fine.

1

There are 1 answers

0
Christopher Francisco On

Solved:

It seems that setting an ID instead of 0 in the second parameter does the trick.

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

Also

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);