Using Firebase Cloud Messaging, when the app is in the background and a message has arrived, the message goes to the system tray. When the user than clicks on the notification, the app gets launched and the launcher activity gets the message data in the Intent
.
In my case, this notification is about some new results, so when pressed, I want to start a ResultsActivity
.
In order to make this happen I do this in the OnStart
of the LauncherActivity
:
Intent intent = getIntent();
String searchId = intent.getStringExtra("search_id");
if(searchId != null){
Intent resultsIntent = new Intent(LauncherActivity.this, ResultsActivity.class);
resultsIntent.putExtra(ResultsActivity.SEARCH_ID_EXTRA, searchId);
startActivity(resultsIntent);
}
This all works great.
The problem is now when clicking on the "up" arrow on the app bar, the app does not go to the parent activity that is defined in the manifest (which is not the launcher activity) but to the launcher activity. This is not surprising since the ResultActivity
is started from the LauncherActivity
, but this is not the wanted behavior. The wanted behavior is for the back arrow to send to the parent activity, which happens to be MainActivity
.
I know there is the TaskStackBuilder
for that kind of stuff, but I don't know how I can apply that pattern to my case here where I start the activity "normally" from another activity and not from some Notification Builder.
Is TaskStackBuilder
the right solution here? If so, how can I change the code above to use it? if not, what is the right solution for this?
What I ended up doing is on the server side, with the firebase cloud messaging admin, instead of including a firebase_admin.messaging.Notification
object in the firebase_admin.messaging.Message
object I am then sending, I just put the notification title and text in the Message
's data, and then build a notification by myself normally in MyFirebaseMessagingService
. Since I'm now building the notification by myself I can add the TaskStackBuilder
normally.
I guess this doesn't really answer the question of how to add a back stack when not using Notification.Builder
, but it's probably a better solution anyway.