Xamarin Forms: Prism: Android: Open a page when clicks on Push Notification

PhaniVPhaniV Member ✭✭

Hi,
We are implementing Push Notification in our app. For iOS, we didn't see much challenges, but with Android, stuck at navigation.
When click on notification alert, need to navigate to MainPage as shown in below code).

It is calling MainPage, but, not initializing ViewModel (MainPageViewModel).
I am not very sure, is this the right way of navigating from Android project.
Similar code worked fine in iOS.
Any hints please?

    protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            Intent = intent;
            var page = new NavigationPage(new MainPage());
            Prism.PrismApplicationBase.Current.MainPage = page;
        }

Best Answer

  • PhaniVPhaniV ✭✭
    Accepted Answer
    protected override void OnNewIntent(Intent intent)
    {
         base.OnNewIntent(intent);
         Intent = intent;
    
         //var page = new NavigationPage(new MainPage());
         //Prism.PrismApplicationBase.Current.MainPage = page;
    
         var container = (App.Current as Prism.Unity.PrismApplication).Container;
         var navigationService = container.Resolve<INavigationService>();
         navigationService.NavigateAsync("/NavigationPage/MainPage");
    }
    

    and added below registration in App.xaml.cs file

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.Register<Prism.Navigation.INavigationService, 
        Prism.Navigation.PageNavigationService>();
    }
    

Answers

  • PhaniVPhaniV Member ✭✭
    Accepted Answer
    protected override void OnNewIntent(Intent intent)
    {
         base.OnNewIntent(intent);
         Intent = intent;
    
         //var page = new NavigationPage(new MainPage());
         //Prism.PrismApplicationBase.Current.MainPage = page;
    
         var container = (App.Current as Prism.Unity.PrismApplication).Container;
         var navigationService = container.Resolve<INavigationService>();
         navigationService.NavigateAsync("/NavigationPage/MainPage");
    }
    

    and added below registration in App.xaml.cs file

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.Register<Prism.Navigation.INavigationService, 
        Prism.Navigation.PageNavigationService>();
    }
    
  • jezhjezh Member, Xamarin Team Xamurai

    Congrats , and thanks for sharing the answer.

  • DevanathanDevanathan Member ✭✭

    Hi @PhaniV

    I am getting NullReferenceException at this line.
    var container = (App.Current as Prism.Unity.PrismApplication).Container;

Sign In or Register to comment.