1

I am trying to implement a basic push notification example using Xamarin Forms with Prism MVVM, Azure & FCM.

I am receiving notification, but couldn't navigate to a specific page when clicked on the notification.

Trying basic functionality when the app is running or in the background (not closed).

It's throwing an exception "PushAsync not supported globally on Android, please use a NavigationPage" at

ExploreXam.App.Current.MainPage.Navigation.PushAsync(page);
[Activity(LaunchMode = LaunchMode.SingleTask, MainLauncher = true]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static readonly string CHANNEL_ID = "explore_xamarin";
    internal static readonly int NOTIFICATION_ID = 1029;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        CreateNotificationChannel();
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        Intent = intent;
        NotificationClickedOn(intent);
    }
    private void NotificationClickedOn(Intent intent)
    {
        if (intent.Action == ExploreXamFirebaseMessagingService.ExploreXamNotification && intent.HasExtra("XamId"))
        {
            var page = new Xamarin.Forms.NavigationPage(new SpecificPage());
            Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(page);
            ExploreXam.App.Current.MainPage.Navigation.PushAsync(page);
        }
    }
}

public partial class App : PrismApplication
{
    public bool navigating;

    public App(IPlatformInitializer initializer = null, bool shallNavigate=false) : base(initializer)
    {
        navigating = shallNavigate;
    }
    protected async override void OnInitialized()
    {
        BlobCache.ApplicationName = "ExploreXam";
        InitializeComponent();
        FlowListView.Init();
        //await  NavigationService.NavigateAsync("LoginPage");
        await NavigationService.NavigateAsync("NavigationPage/LoginPage");
    }
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //mapping 
    }
}

Any idea that would help out, please?

Explore
  • 115
  • 13

3 Answers3

1

You could access Prims's NavigationService instance to achieve what you're trying to do. But, it's a protected property. So, first you'd have to expose it through your App class as below :

public new INavigationService NavigationService => base.NavigationService;

Now, you can access the NavigationService from anywhere in your app by simply referencing it through your App as below:

(Xamarin.Forms.Application.Current as App).NavigationService.NavigateAsync("your/page/path");

So, your App class would look something like this:

public partial class App : PrismApplication
{
    public new INavigationService NavigationService => base.NavigationService;
    public bool navigating;

    public App(IPlatformInitializer initializer = null, bool shallNavigate=false) : base(initializer)
    {
        navigating = shallNavigate;
    }

    protected async override void OnInitialized()
    {
        BlobCache.ApplicationName = "ExploreXam";
        InitializeComponent();
        FlowListView.Init();
        //await  NavigationService.NavigateAsync("LoginPage");
        await NavigationService.NavigateAsync("NavigationPage/LoginPage");
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //mapping 
    }
}

And your NotificationClickOn function would become something like :

private async void NotificationClickedOn(Intent intent)
{
    if (intent.Action == ExploreXamFirebaseMessagingService.ExploreXamNotification && intent.HasExtra("XamId"))
    {
        var navigationService = (Xamarin.Forms.Application.Current as ContosoCookbook.App).NavigationService;
        await navigationService.NavigateAsync("YourNavigationPage/SpecificPage");
    }
}
chaosifier
  • 2,666
  • 25
  • 39
0

The reason this is happening is because your Application.Current.MainPage is not a Navigation page but a ContentPage (i assume)

Wrap your initial MainPage in a NavigationPage as show below and it should work

In your App.xaml.cs

     MainPage= new NavigationPage(new FirstPage());
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
0

I agree with @chaosifier. Create a public INavigationService in your App.xaml.cs file and then in the OnInitialized() method make the public property = the the base.NavigationService;

public INavigationService PrismNavigation { get; private set; }

protected override async void OnInitialized()
{
    InitializeComponent();
    PrismNavigation = base.NavigationService;
}

Then from the MainActivity.cs file you can navigate using something like this

(Xamarin.Forms.Application.Current as App).PrismNavigation.NavigateAsync(nameof(ShowAlertsDetailPage));

I hope this helps.