putExtra using pending intent not working
T

4

22

I have written a code in my GCMIntentservice that sends push notifications to many users. I use the NotificationManager that will call DescriptionActivity class when the notification is clicked. I also send the event_id form the GCMIntentService to the DescriptionActivity

protected void onMessage(Context ctx, Intent intent) {
     message = intent.getStringExtra("message");
     String tempmsg=message;
     if(message.contains("You"))
     {
        String temparray[]=tempmsg.split("=");
        event_id=temparray[1];
     }
    nm= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    intent = new Intent(this, DescriptionActivity.class);
    Log.i("the event id in the service is",event_id+"");
    intent.putExtra("event_id", event_id);
    intent.putExtra("gcmevent",true);
    PendingIntent pi = PendingIntent.getActivity(this,0, intent, 0);
    String title="Event Notifier";
    Notification n = new Notification(R.drawable.defaultimage,message,System.currentTimeMillis());
    n.setLatestEventInfo(this, title, message, pi);
    n.defaults= Notification.DEFAULT_ALL;
    nm.notify(uniqueID,n);
    sendGCMIntent(ctx, message);

}

Here the event_id that I'm getting in the above method is correct i.e I always get the updated one. But in the code below (DescriptionActivity.java):

    intent = getIntent();
    final Bundle b = intent.getExtras();
    event_id = Integer.parseInt(b.getString("event_id"));

The event_id here is always "5". No matter what I putExtra in the GCMIntentService class,the event_id I get is always 5. Can somebody please point out the problem? Is is because of the pending intent? If yes, then how should I handle it?

Tecla answered 4/5, 2013 at 16:46 Comment(0)
N
43

The PendingIntent is reused with the first Intent you provided, that's your problem.

To avoid this, use the flag PendingIntent.FLAG_CANCEL_CURRENT when you call PendingIntent.getActivity() to actually get a new one:

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Alternatively, if you just want to update the extras, use the flag PendingIntent.FLAG_UPDATE_CURRENT

Nonfeasance answered 4/5, 2013 at 17:37 Comment(1)
if it's was not clear: PendingIntent pi = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);Tatting
L
12

The PendingIntent is reused with the first Intent you provided, as it was said by Joffrey. You can try use the flag PendingIntent.FLAG_UPDATE_CURRENT.

PendingIntent pi = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Lemuelah answered 30/10, 2014 at 19:46 Comment(0)
P
5

Maybe you are still using the old intent. try this:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    //try using this intent

    handleIntentExtraFromNotification(intent);
}
Preglacial answered 11/12, 2015 at 15:41 Comment(1)
Thank you very much. I have been battling with thisBray
I
0

when i used Pending intent to push the notification i used the code:

//current activity
val intent = Intent(this, TargetActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    intent.putExtra("value", value)
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

//TargetActivity
val fetchedValue = intent.getStringExtra("value").toString()

But by using this code in the TargetActivity fetchedValue =

null

here i update the code and get the fetchedValue

//current activity
val intent = Intent(this, TargetActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    intent.putExtra("value", value)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        )
    } else {
        PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }

//TargetActivity
val fetchedValue = intent.getStringExtra("value").toString()

this code prevents from the app crashes.

Incurvate answered 17/2 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.