如何防止OneSignal重新初始化应用程序?

时间:2019-03-05 14:54:07

标签: xamarin xamarin.forms onesignal

我正在推送通知中发送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

0 个答案:

没有答案