Xamarin形式:Prism:Android:MainActivity:单击“推送通知”:Android上不全局支持PushAsync,请使用NavigationPage

时间:2019-03-23 07:59:19

标签: xamarin.forms push-notification xamarin.android firebase-cloud-messaging prism

我正在尝试使用实现一个基本的推送通知示例 带有Prism MVVM,Azure和FCM的Xamarin Forms。

我正在接收通知,但是单击该通知时无法导航到特定页面。

在应用运行或在后台(未关闭)时尝试基本功能。

在以下位置抛出异常“ Android上不全局支持PushAsync,请使用NavigationPage”

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 
    }
}

有什么想法可以帮到您吗?

3 个答案:

答案 0 :(得分:0)

发生这种情况的原因是因为您的Application.Current.MainPage不是导航页,而是ContentPage(我认为)

将您的初始MainPage包裹在NavigationPage中,如下所示,它应该可以工作

在您的App.xaml.cs

     MainPage= new NavigationPage(new FirstPage());

答案 1 :(得分:0)

您可以访问Prims的NavigationService实例来实现您想要的操作。但是,这是一个受保护的财产。因此,首先您必须通过下面的App类公开它:

public new INavigationService NavigationService => base.NavigationService;

现在,您可以通过在应用程序中的任何位置访问导航服务,方法如下:

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

因此,您的App类看起来像这样:

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 
    }
}

您的NotificationClickOn函数将变为:

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");
    }
}

答案 2 :(得分:0)

我同意@chaosifier。在App.xaml.cs文件中创建一个公共INavigationService,然后在OnInitialized()方法中将public属性设为base.NavigationService;

public INavigationService PrismNavigation { get; private set; }

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

然后从MainActivity.cs文件中,您可以使用类似的内容进行导航

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

我希望这会有所帮助。