Xamarin android - push notification Intent extras lost

Viewed 311

I have follow this guide to setup push notification in my Xamarin Forms app.

I have an app with two activity, one splash activity

Activity(Theme = "@style/SplashTheme",
        MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait, NoHistory = true)]
    public class SplashAct: Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            StartActivity(typeof(MainActivity));
            Finish();
        }
    }

and the MainActivity

 [Activity(
        ClearTaskOnLaunch = false,
        FinishOnTaskLaunch = false,
        Theme = "@style/MainTheme",
        LaunchMode = LaunchMode.SingleTask,
        ScreenOrientation = ScreenOrientation.Portrait,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public partial class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, Android.Gms.Tasks.IOnSuccessListener

in the main activity I have override the OnNewIntent method

     protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            ProcessNotificationActions(intent);
        }

and I have tried to send a notification using a web api published on Azure with this body:

{
    "text": "Test msg",
    "action": "action_a"
}

on device the text is correctly shown, but when the code reach the OnNewIntent method the intent has not extras so I lost the "action" value.

How can I solve this?

NB. I can't use LaunchMode = LaunchMode.SingleTop otherwise the OnNewIntent is not called, the OnCreate is called and the app shows a blank screen. This happens only if the application is in background or closed. If it is in foreground all works fine.

Attached a sample, a video and info to make the POSTMAN\RESTER request.

Archive.zip

1 Answers

Your SplashAct is set to be the MainLauncher which will be fired up when your push notification is tapped. The Intent recieved there first will not automatically pass the extras on when it launches MainActivity. You can manually transfer them over when you call StartActivity in your SplashAct:

protected override void OnCreate(Bundle savedInstanceState)
{   
    base.OnCreate(savedInstanceState); 
    var intent = new Intent(Application.Context, typeof(MainActivity));
    intent.PutExtras(Intent);
    StartActivity(intent);
    Finish();
}
Related