【问题标题】:Xamarin Forms:Prism:Android:MainActivity: Click on Push Notifications: PushAsync not supported globally on Android, please use a NavigationPageXamarin Forms:Prism:Android:MainActivity:单击推送通知:Android 上不支持全球范围内的 PushAsync,请使用 NavigationPage
【发布时间】:2019-08-14 03:51:09
【问题描述】:

我正在尝试使用 Xamarin Forms 与 Prism MVVM、Azure 和 FCM。

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

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

它抛出异常“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 
    }
}

请问有什么好办法吗?

【问题讨论】:

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


    【解决方案1】:

    您可以访问 Prims 的 NavigationService 实例来实现您想要做的事情。但是,它是受保护的财产。因此,首先您必须通过您的 App 类公开它,如下所示:

    public new INavigationService NavigationService => base.NavigationService;
    

    现在,您可以从应用中的任何位置访问 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】:

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

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

      在您的 App.xaml.cs 中

           MainPage= new NavigationPage(new FirstPage());
      

      【讨论】:

      • @Hakim,我添加了 App.cs 以供参考。仍然面临同样的问题。
      【解决方案3】:

      我同意@chaosifier。在 App.xaml.cs 文件中创建公共 INavigationService,然后在 OnInitialized() 方法中使公共属性 = 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));
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-11-18
        • 2020-05-21
        • 2014-08-28
        • 2018-01-13
        • 2017-12-19
        • 2020-05-04
        • 2017-09-15
        • 2018-03-01
        • 1970-01-01
        相关资源
        最近更新 更多