I am trying to build a notification service using the firebase Messaging Service. Problem is When I am clicking the notification It takes me to the correct intent but my App bar title is not updating properly. I am passing the username in the intent but in getExtra() of the Intent, it returns null. I am getting the correct userId but not getting correct name. My code for notification builder looks like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.chitchat_icon)
.setContentTitle(notification_title)
.setAutoCancel(true)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", user_id);
resultIntent.putExtra("user_name",user_name);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
stackBuilder.addParentStack(MainActivity.class);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
I am getting Intent Extra and setting it as the title for the action bar:
//change in postion
mChatUser = getIntent().getStringExtra("user_id");
String userName = getIntent().getStringExtra("user_name");
...
mTitleView.setText(userName);
My firebase js function to generate notification looks like this:
const fromUser = admin.database().ref(`/message_notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {
const from_user_id = fromUserResult.val().from;
const message = fromUserResult.val().body;
console.log('You have new notification from : ', from_user_id);
const userQuery = admin.database().ref(`/Users/${from_user_id}/name`).once('value');
return userQuery.then(userResult => {
const userName = userResult.val();
const deviceToken = admin.database().ref('/Users/'+ user_id +'/device_token').once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title: userName,
body: message,
icon: "default",
click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
},
data : {
user_id : from_user_id,
user_name: userName
}
};
}
Can anyone help me how to solve this problem? Can't Locate the issue. Thanks.
Update:
I also want to share that it works fine if there are two different devices but if I log in and send notification logout and receive notification it can't retrieve username.