stealthjong
stealthjong

Reputation: 11093

Android onNewIntent always receives same Intent

I have 2 Notifications: one for incoming messages, one for outgoing messages. On Notification click, it sends the PendingIntent to self. I put in an extra value to determine which of the Notifications was clicked:

private static final int INID = 2;
private static final int OUTID = 1;

private void update(boolean incoming, String title, String message, int number) {
    notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, Entry.class);
    intent.putExtra((incoming ? "IN" : "OUT"), incoming);
    PendingIntent pi = PendingIntent.getActivity(Entry.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification noti = new Notification(incoming ? R.drawable.next : R.drawable.prev, incoming ? "Incoming message" : "Outgoing message", System.currentTimeMillis());
    noti.flags |= Notification.FLAG_NO_CLEAR;
    noti.setLatestEventInfo(this, title, message, pi);
    noti.number = number;
    notificationManager.notify(incoming ? INID : OUTID, noti); 
}

And capture the Intent in the onNewIntent method:

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    if (intent.getExtras() != null) 
        for (String id : new ArrayList<String>(intent.getExtras().keySet())) {
            Object v = intent.getExtras().get(id);
            System.out.println(id + ": " + v);
        }
    else
        log("onNewIntent has no EXTRAS");
}

plus the manifest line that makes sure that there's only one task (in activity tag):

android:launchMode="singleTop" 

I logged that it runs through the onNewIntent method, but always use the same intent (IE if I click either the IN or OUT notification, the intent extra always contains the same bundle(logs: OUT: false)). It's always the Intent that was created last, which I found out because the initialization of both intents happens in another sequence than when they are changed:

private void buttonClick(View v) {      
    update(true, "IN", "in", 1);
    update(false, "OUT", "out", 3);
}

private void setNotificationSettings() {
    update(false, "IN", "===out message===", 0);
    update(true, "OUT", "===in message===", 0);
}

Why do I always receive the same (last created) Intent?

Upvotes: 2

Views: 2556

Answers (1)

Niranj Patel
Niranj Patel

Reputation: 33238

You are passing same requestcode for all intent that why you reciev last intent everytime, so you have to pass different requestcode in pending intent..

like as below code

Your code:

 PendingIntent pi = PendingIntent.getActivity(Entry.this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

Need to change:

PendingIntent pi = PendingIntent.getActivity(Entry.this, your_request_code, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 9

Related Questions