Building a task stack for activity started after Firebase Notification - android

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.

Related

In build notification bar in android

Any one can tell me how could i get inbuild notification listner in android. What all i want is if user put password on screen and if then some notification arrives. and if user click on notification, I want password field should be reset.
See http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification to create a local notification.
In the intent you use for the notification add an extra bit of data that you define (e.g. "ClearPassword" as a boolean of true).
In your activity, check for extras and your specific extra and if it is set then you can clear the field.
Why don't you just clear the password field in the onPause method of the activity? If the user clicks on the notification, the pending intent will launch a new activity (most in the cases from another application). When your activity is no longer the foreground activity, its onPause method will be called.

"smart" android activation from a push notification

I have a simple task - activate an app from the push on a particular activity (not on the start activity)
Imaging I have 3 activities in the app:
A (splash)
B (items list)
C (selected item details)
Some pre-requirements:
With push I'm getting the id of item to select.
On the splash I'm forcing an authentication.
One of the conditions - I couldn't move authentication let's say to another activity or to application service for example.
Now I could create several statements. When I tap on push to activate the app:
When push is arrived the PushIntentService generates a notification which specifies item id in intent extras If the app was terminated I should start the app from the activity A (to force authentication)
If the app was backgrounded (works in background) I should re-activate it at the same place (to skip re-authentication)
Once the app is activate I will navigate to Activity C with item id fetched from extras.
Right now I'm using the following code to generate the notification (item 1, Xamarin.Android syntax):
var resultIntent = new Intent(Application.Context, typeof(SplashScreen));
resultIntent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
if (extras.ContainsKey("ItemId"))
{
var itemId = extras["ItemId"];
resultIntent.PutExtra("ItemId", itemId);
}
var resultPendingIntent = PendingIntent.GetActivity(Application.Context, 0, resultIntent, 0);
builder.SetContentIntent(resultPendingIntent);
var notification = builder.Build();
This notification works absolutely fine in all cases but I see here one issue.
I'm restarting the app from the very beginning every time I'm tapping on a notification.
What I want is when the app is backgrounded I need just to activate it (like iOS does) and navigate to required page (faster activation and avoid re-authentication).
How can I achieve this and modify the code above?
Create a new Activity for this. The notification should start that Activity (without any flags).
In onCreate() of this new Activity, do something like this:
super.onCreate(...);
if (!isTaskRoot() && alreadyAuthenticated) {
// Go directly to details page
Intent redirectIntent = new Intent(this, Details.class)
redirect.putExtra("id", itemId);
startActivity(redirectIntent);
else {
// This means the app was not running, so redirect to Splash
Intent redirectIntent = new Intent(this, Splash.class)
startActivity(redirectIntent);
}
finish();
isTaskRoot() will return true if the app was not running when the user clicked on the Notification. If the app was already running, it should return false.
To test if you are already authenticated, you could call a static method or check a static variable or maybe you have some other method of doing this. Depending on what you want the Activity stack to look like if the user was already in the "item details" or "item list" activites, you may want to add SINGLE_TOP and/or CLEAR_TOP flags when redirecting to the item details Activity.
Hopefully you get the point.
I found the answer on how to simulate launcher icon tap intent. I'm using it to create pending intent for my push:
var launchIntent = PackageManager.GetLaunchIntentForPackage(PackageName);
This is exactly what I wanted and it works perfectly fine in my case.

Android deep-linking. Intent doesn't reset when app is opened from history

I have a problem regarding Android task and intent management.
Scenario
User gets a push with a deep-link into the app.
We show a notification putting the URI into the Intent Data.
User clicks the notification and is taken into the app and redirected to some Feature1Activity described by the deep-link.
User looks around, and backs out of the app.
Later, user opens the app from history (long-press home or multitasking button).
Now the same intent that were used from the notification (with the deep-link in the Intent Data) is used to start the app.
Hence, user is taken into the Feature1Activity again.
Problem:
Starting the app from history (long-press home or multitasking button) does not reset the Task (which it does when launching from app icon).
I understand that starting an app from history is not supposed to reset the task since it is intended to be used as "get-right-back-where-you-were". However, in my case this is an issue since the launch of the app from a notification is a one time thing.
Anyone else encountered this problem? Anyone know any solution?
More in-depth
The intent inside the PendingIntent is built like this:
Intent intent = new Intent (Intent.ActionView);
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.setData (Uri.Parse (DEEP_LINK_URL));
I found out about the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET just this day and really thought that it would git rid of my problem but it made no difference.
There are three activities of interest:
SplashActivity (main launcher & listener of the deep-linking schema -- this activity just redirects either to login or OverviewActivity)
OverviewActivity (authorized user's main activity)
Feature1Activity (any feature that the deep-link is pointing to)
What happens when the user clicks the notification is that the SplashActivity acts as a listener for the schema and converts the deep-link url to two intents to start up OverviewActivity and Feature1Activity using Activity.startActivities (Intent[]).
When I look at the intent from the notification inside SplashActivity it always contain the deep-link in the Data.
One work around
There is a work around, setting some booleanExtra field to the notification intent (for instance "ignoreWhenLaunchedFromHistory" = true) and then check in SplashActivity before redirecting
boolean fromHistory = (getIntent().getFlags() & FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY;
if (fromHistory && getIntent().getBooleanExtra ("ignoreWhenLaunchedFromHistory", false))
// Don't follow deep-link even if it exists
else
// Follow deep-link
Except that it hackish and ugly, can you see any problems with this work around?
EDIT: The work around only works when I am responsible for sending the Intent with the deep-link. Since no external source can know about the "ignoreWhenLaunchedFromHistory" extra.
From what I get, maybe using android:excludeFromRecents="true"on your manifest (as a property for the Activity declaration) might ameliorate the issue?

Activity opened twice

I have an application that uses Urban Airship for push notification. When a notification arrives and the user clicks on it, activity A in my application should open and do something.
I've installed the BroadcastReceiver as is shown in the docs, and it's almost working.
When my app is in the foreground I don't let the user see the notification at all, and just handle it automatically.
When my app is not running at all, the activity opens up just fine.
When my app is in the background (which always happens when A is the top activity), a second instance of Activity A is created.
This is, of course, a problem. I don't want two A activities, I just want one of them. Here's the relevant BroadcastReceiver code:
#Override
public void onReceive(Context ctx, Intent intent)
{
Log.i(tag, "Push notification received: " + intent.toString());
String action = intent.getAction();
int notificationId = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, -1);
if(action.equals(PushManager.ACTION_NOTIFICATION_OPENED))
{
Intent intentActivity = new Intent(ctx, ActivityA.class);
intentActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
UAirship.shared().getApplicationContext().startActivity((intentActivity);
}
}
UPDATE:
I tried to bypass this bug by calling System.exit(0) when the user presses Back on Activity A. The process ended, but then it was restarted immediately! My BroadcastReceiver is not called again in the second instance. What's happening?
UPDATE 2:
#codeMagic asked for more information about the app and activity A.
This app lets its user review certain items and comment on them. Activity A is started when the app is launched. If the user's session isn't valid any more, a Login activity is started. Once the user logs in, activity A becomes active again. A only has a "No items to review" message and a "Try now" button.
When the user logs in, the server starts sending push notifications whenever a new item is available for review. When the app gets the notification, activity A accesses the server and gets the next item to review. The item is shown in activity B. Once the review is submitted to the server, activity B finishes and activity A is again the top activity.
The server knows when a user is reviewing an item (because activity A fetched it), and doesn't send push notifications until the review is submitted - meaning a notification can't come if the user isn't logged in or if the user is viewing activity B.
While I agree there is a subtle race condition here, it is not causing the problem I'm seeing - in testing I am 100% positive there's no race condition - the push notification is only sent after Activity A becomes active again.
The solution was to add a launchMode='singleTask' to the activity in AndroidManifest.xml . As a result, instead of a new activity, onNewIntent of the same activity instance is called.
You can use one of several Intent Flags. FLAG_ACTIVITY_REORDER_TO_FRONT being one of them. This will bring the Activity to the front of the stack if it is already in the stack and if not then it will create a new instance. I believe you will still need FLAG_ACTIVITY_NEW_TASK if you aren't calling it from an Activity
Intent.FLAG_ACTIVITY_CLEAR_TOP should also work. But this will clear any other Activities on the stack. It just depends on what other functionality you need. Look through the Intent Flags and see which of these will work best for you
There are multiple scenarios when this could happen. One of them can be handled this way. Please see my answer here: https://stackoverflow.com/a/44117025/2959575
Ok, two notes on this :
You can register a broadcast receiver via the manifest so it is independent of any parts of your app. and use a Singleton pattern (keep a static reference to your activity somewhere in your app) that way you can check if their is an activity viewing or not and process accordingly.
// your activity A
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
myActivityReference = this;
}
public void onPause() {
super.onPause();
if (isFinishing()) {
myActivityReference = null;
}
}
or you can keep everything as it is and use activity lunching modes flags in your manifest such as singleTop, singleInstance ... etc. take a look here android activity lunch modes

Continuing the same activity when user clicks on Notification

i am making an application in which Broadcast listener is starting an activity and notification is displayed.
when a user clicks on home button it goes to the home screen.
But if user clicks on Notification icon then activity state is lost :( .
Please help me how to continue with my activity when user clicks on notification.
I don't display a notification if the application that is doing the notifying is in the foreground; I just update the UI of the activity and let it be self-evident.
Try this:
Intent resultIntent = this.getIntent();
It gets the started intent from main activity. Worked for me.
I suggest using an alert dialog for notifications link here. This will keep your notification on the same screen and in the same activity.
If you want to keep any information when you press home you may use android storage for that and here is the link for that. Storage will be able to hold data that you need from other activity classes.
I hope this helps you.
When building your Intent for the PendingIntent you send with the notification, add the flag Intent.FLAG_ACTIVITY_NEW_TASK.
If the activity is already on the history stack, it will be resumed and a call to onNewIntent() will be triggered. If it's not, the activity will be started with a blank slate.
Try this
Intent.FLAG_ACTIVITY_TASK_ON_HOME

Categories

Resources