Search code examples
androidandroid-intentandroid-activitypush-notificationback-stack

"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:

  1. A (splash)
  2. B (items list)
  3. 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:

  1. 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)
  2. If the app was backgrounded (works in background) I should re-activate it at the same place (to skip re-authentication)
  3. 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?


Solution

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