My xamarin forms's android app is crashing when click on push notification when app closed

ShahriatHossainShahriatHossain USMember ✭✭
edited March 2017 in Xamarin.Forms

When I click on push notification pop up when my app is closed then my app is crashing. I am handling my code below:

[Activity(Label = "Uno", Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.tabs;
            ToolbarResource = Resource.Layout.toolbar;
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            if (IsPlayServicesAvailable())
            {
                var intent = new Intent(this, typeof(RegistrationIntentService));
                StartService(intent);
            }

     PushToDetailPageOnAppCloseMode();

            LoadApplication(new App());
        }

private void PushToDetailPageOnAppCloseMode()
        {
            if (Intent != null)
            {
                GetExtrasFromNotification(new Intent());
            }

        }

        protected override void OnNewIntent(Intent intent)
        {
            if (intent != null)
            {
                GetExtrasFromNotification(intent);
            }
        }

        private void GetExtrasFromNotification(Intent intent)
        {
            var extras = (!intent.HasExtra("channelInfo"))? Intent.GetStringExtra("channelInfo"): intent.GetStringExtra("channelInfo");
            if (string.IsNullOrEmpty(extras)) return;

            var channelInfo = JsonConvert.DeserializeObject<PushNotificationInfo>(extras);
            UserSession.Current.SetPushChannelName(channelInfo.ChannelName);

            int channelId = channelInfo.ChannelId;
            NavigateToDetailPage(channelId);
        }

        private static void NavigateToDetailPage(int channelId)
        {
            if (channelId > 0)
            {
                UserSession.Current.SetPushChannelId(channelId);

                var page = new ChannelDetailViewer();

                Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
                {
                    await App.Current.MainPage.Navigation.PushAsync(page, false);
                });
            }
        }
}

in this case I am maintaining OnNewIntent method to handle the push notification when my app stay both on Background and ForeGround which is working good but when my app goes to stopped mode or closed mode then my app is crashing and for close mode I am using OnCreate method of my MainActivity class as you can see above.

Can any one suggest me what should I do. My objective is when user click on push notification pop up it will redirect to a specific page whatever mode app goes.

Answers

Sign In or Register to comment.