我正在推送通知中发送additionalData
,以导航到应用程序中的特定页面。在App
构造函数中,除了初始化MainPage
之外,我还要初始化Onesignal:
OneSignal.Current.StartInit("aaaaaa-aaa-aaa-aa-
aaaaaaaa").HandleNotificationOpened(NotificationOpened).InFocusDisplaying(OSInFocus
DisplayOption.Notification).EndInit();
然后我进入NotificationOpened
方法:
private async void NotificationOpened(OSNotificationOpenedResult result)
{
var pushData = result.notification.payload.additionalData;
if (pushData["is_ad"].ToString() == "1")
{
string ad_id = pushData["ad_id"].ToString();
await Current.MainPage.Navigation.PushAsync(new AdPage(ad_id));
}
}
问题在于,它永远无法正常工作,因为当点击推送通知时,将调用App
的构造函数(在调用NotificationOpened
之后),并且将MainPage
设置为应用程序的主页。
App.xaml.cs:
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace MyApp
{
public partial class App : Application
{
public static bool IsConnected
=> Connectivity.NetworkAccess == NetworkAccess.Internet;
public App()
{
// Initialize Live Reload.
#if DEBUG
LiveReload.Init();
#endif
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("licenseKey");
OneSignal.Current.StartInit("aaaaaa-aaa-aaa-aa-
aaaaaaaa").HandleNotificationOpened(NotificationOpened).InFocusDisplaying(OSInFocusDisplayOption.Notification).EndInit();
InitializeComponent();
Settings.Init();//localization and getting user data from cache
if (Settings.IsLoggedIn)
MainPage = new NavigationPage(new MainPage());
else
MainPage = new NavigationPage(new LoginPage());
}
在app.xaml中,我有一些样式(其中一种针对NavigationPage
)