Android: Part of my extras are getting lost in pending intent
Asked Answered
C

3

12

I am making an Android app for the first time and I am having this problem. When a user A sends a friend request to another user B, user B gets a notification. I wish when the user B click on the notification to be redirected to user A profile.

The main activity (Home in app) opens different fragments depending on from where the user is coming. A user profile is one such fragment.

This is how I am sending the notification from a presenter class for Main Activity.

Intent notificationIntent = new Intent(activity.getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Bundle extras = new Bundle();
extras.putString("name" ,this.friendRequestFromUserName.toString());
extras.putString("email", this.friendRequestFromUserEmail);
extras.putString("notification", "notificationFriendRequest");

notificationIntent.putExtras(extras);

System.out.println("PUTTING EXTRA");
System.out.println(notificationIntent.getExtras().toString());
// output here is:
//Bundle[{name=The Lion King, [email protected], notification=notificationFriendRequest}]

NotificationCompat.Builder builder = new NotificationCompat.Builder(activity)
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("New friend!")
    .setContentText(this.friendRequestFromUserName + " wants to be friends.")
    .setAutoCancel(true)
    .setOnlyAlertOnce(true)
    .setOngoing(false)
    .setContentIntent(PendingIntent.getActivity(activity.getApplicationContext(), 0, notificationIntent, 0));

builder.setOngoing(false);
builder.setAutoCancel(true);

Notification not = builder.build();
not.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager manager = (NotificationManager)this.activity.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(123, not);

However in the main activity when user clicks on the notification only part of the Bundle is "arriving". Here are the ways I tried to get the extras:

    extraName = intent.getStringExtra("name");
    extraMail = intent.getStringExtra("email");
    extra = intent.getStringExtra("notification");

and

    System.out.println(intent.getExtras().toString());
    //Bundle[{notification=notificationFriendRequest}]

I also tried putting arrays as extra with the same result. Since part of my extra is arriving, but other is not, and I couldn't find similar topic and problem I decided to ask if somebody can help or explain why or how this can happen. Thanks.

Cubeb answered 15/1, 2016 at 11:8 Comment(6)
did you try to use unique requestCodes in getActivity?Mariellamarielle
No, I have not. Now that I tried generating random int for request code seems to not sending any extras at all.Cubeb
Well I did, and when I am giving number different than 0 it is not sending any extras (I tried generating random number and also just putting other numbers than zero). I am also not sure what is the purpose of this code :/Cubeb
so use the combination of unique requestCode and PendingIntent.FLAG_UPDATE_CURRENT flagMariellamarielle
OH THANK YOU! That made it work!Cubeb
and if you are using just one notification at given time (id = 123) you can skip different requestCodes, just use 0 or whateverMariellamarielle
C
19

So as pskink suggested I added unique requestCode and PendingIntent.FLAG_UPDATE_CURRENT flag and that solved it.

.setContentIntent(PendingIntent.getActivity(activity.getApplicationContext(), Math.abs(generator.nextInt()), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Since I don't see how to mark a suggestion as an answer 'cause that is my first question here I am answering it myself. Thank you again, pskink!

Cubeb answered 15/1, 2016 at 13:7 Comment(2)
and if you are using just one notification at given time (id = 123) you can skip different requestCodes, just use 0 or whateverMariellamarielle
Android, never stops surprising me.Recipient
A
1

You are getting values in wrong way. As you are passing everything as a bundle in Intent , then first you have to get Bundle object first from intent like this

Bundle extras = getIntent().getExtras(); and then get other strings from bundle object

Autoclave answered 15/1, 2016 at 11:15 Comment(2)
When I am printing the extras like so System.out.println(intent.getExtras().toString()); this have the same result - the bundle only have one of three extrasCubeb
instead of printing try debugging when you get extras. i don't think you're print line will print all extras in one goAutoclave
I
1

try to get fist the bundle object and then get the values like this, I hope this can help you.

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     try{
          Bundle data= this.getIntent().getExtras();
          extraName = data.getString("name");
          extraMail = data.getString("email");
          extra = data.getString("notification");
     }catch(Exception e){
          e.printStackTrace();
     }
}
Impressment answered 15/1, 2016 at 11:29 Comment(3)
Maybe the error can be in notificationIntent.setFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP); because If set, the activity will not be launched if it is already running at the top of the history stack. I will try to use notificationIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); insteadImpressment
thank you, but this is also not the case (tried both flags together and separately). Note that the activity is actually launched, checks if there is extras is the intent, finds only one and opening the user fragment after that(if there is no extra the behaviour is different), only that user is left nameless and email-less.Cubeb
And I also tried a bunch of other flags with no different result.Cubeb

© 2022 - 2024 — McMap. All rights reserved.