我正在尝试使用带有Prism MVVM,Azure和FCM的Xamarin Forms实现基本推送通知示例。
我收到通知,但在点击通知时无法导航到特定页面。
在应用程序运行时或在后台(未关闭)时尝试基本功能。
它引发了一个例外“Android上全局不支持PushAsync,请使用导航页面”
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
}
}
请问有什么好处吗?
发生这种情况的原因是因为你的Application.Current.MainPage
不是导航页面而是ContentPage(我假设)
将您的初始MainPage
包装在NavigationPage中,如下所示,它应该可以工作
在App.xaml.cs中
MainPage= new NavigationPage(new FirstPage());
您可以访问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");
}
}
我同意@chaosifier。在App.xaml.cs文件中创建一个公共INavigationService,然后在OnInitialized()方法中创建public property = the 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));
我希望这有帮助。