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.
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!
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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With