Search code examples
androidandroid-fragmentsandroid-intentandroid-pendingintentextras

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


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.


Solution

  • 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!