Skip to content

[Android] Getting NotificationTapped to work with splash screen

Elvin (Tharindu) Thudugala edited this page Aug 16, 2023 · 1 revision

Since Xamarin.Forms does not support splashscreen itself, adding new Splashscreen Activity often "breaks" the component initialization at startup

In order to make the NotificationTapped event working, you must pass the data from the original Intent (SplashScreen Activity) to your Main Activity intent.

[Activity(Theme = "@style/SplashTheme",
        NoHistory = true ,
        MainLauncher = true)]
    public class SplashActivity : Activity
    {        
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var actionId = Intent.GetIntExtra(LocalNotificationCenter.ReturnRequestActionId, -1000);
            var mainIntent = new Intent(Application.Context, typeof(MainActivity));

            mainIntent.SetFlags(ActivityFlags.SingleTop);
            if (actionId != -1000)
            {
                var data = Intent.GetStringExtra(LocalNotificationCenter.ReturnRequest);
                mainIntent.PutExtra(LocalNotificationCenter.ReturnRequestActionId, actionId);
                if (!string.IsNullOrEmpty(data))
                {
                    mainIntent.PutExtra(LocalNotificationCenter.ReturnRequest, data);
                }
            }

            StartActivity(mainIntent);
        }
    }

Also look at https://progrunning.net/best-way-to-create-a-splash-screen-in-xamarin-forms-android-project/

You can override OnCreate or OnResume in SplashActivity. Also Add LaunchMode = LaunchMode.SingleTask to MainActivity this is to prevent the app from relaunching upon notification tapped

Thank you @jslachta, @thomasgalliker, @angelru and @maxchu92