xamarinxamarin.formsnotificationsxamarin.shell

Xam.Plugins.Notifier: Android Tap in Xamarin.Forms.Shell


Using Xam.Plugins.Notifier for Local Notifications I am able to show a notification in Android. However, when I tap the notification, it reloads the application. Similar to how a remote notification would.

I handle the OnNewIntent() in the MainActivitiy.cs but it never fires.

How do I tap a Local Notification made by Xam.Plugins.Notifier so that OnNewIntent() fires and I can show a Shell item?

protected async override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    var title = intent.GetStringExtra("title");
    if (title != null)
    {
        await Shell.Current.GoToAsync("Tools/Sales");
    }
}

I have a SplashScreen Activity that actually starts the app:

How do I pass the Intent that starts the activity to the MainActitity from the Splash

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
public class SplashActivity : AppCompatActivity
{


    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
    {
        base.OnCreate(savedInstanceState, persistentState);
    }

    // Launches the startup task
    protected override void OnResume()
    {
        base.OnResume();
        StartActivity(new Intent(Application.Context, typeof(MainActivity)));
    }

    public override void OnBackPressed() { }


}

Solution

  • On the MainActivty class, set its LaunchMode to SingleTop so the OS will reuse the activity IF it is all already running vs. starting a new one:

    [Activity(Label = "Your Xamarin Forms App", MainLauncher = true, Icon = "@mipmap/icon", LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
    

    NOTE: There are two entry points into MainActvity for the notification intent

    a. If the activity is already running, OnNewIntent will be called with the intent that you setup on the notification.

    b. If your app is not running, MainActvity will be created as a normal startup but will include the intent from from the notification, so check the intent in the OnCreate.