xamarin 推送通知 ios 单击推送通知时页面导航不起作用

xamarin push notification ios Page navigation not working when push notification click

我有 Xamarin 表单中的 azure 推送通知。当收到推送通知时,我需要在我的应用程序中打开一个页面。它在 android 中运行良好。在 iOS 中,当应用程序打开时它不起作用。当应用程序在后台运行时,它会在 iOS.

中找到

这是我的 App Delegate 代码

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
   Action<UIBackgroundFetchResult> completionHandler)
    {

        try
        {
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
            NSError error;
            NSData notification = NSJsonSerialization.Serialize(userInfo["action"], NSJsonWritingOptions.PrettyPrinted, out error);
            string notificationString = NSString.FromData(notification, NSStringEncoding.UTF8).ToString();

            var NotificationObj = JsonConvert.DeserializeObject<NotificationData>(notificationString);
            NotificationService.pushPageName = NotificationObj.Notification[0].PageName.ToString();
            NotificationService.pushAppName = NotificationObj.AppName.ToString();
            NotificationService.OpenPages = NotificationObj.Notification[0].OpenPage;
            NotificationService.Notification = notificationString;

            if (UIApplication.SharedApplication.ApplicationState.Equals(UIApplicationState.Active))
            {
                //App is in foreground. Act on it.
                var application1 = new App(NotificationService.pushPageName, NotificationService.pushAppName, NotificationService.Notification);
                LoadApplication(application1);
            }
            else
            {
              //  var application1 = new App(NotificationService.pushPageName, NotificationService.pushAppName, NotificationService.Notification);
            //    LoadApplication(application1);
            }
        }
        catch (Exception ex)
        {
            //LogInfo.ReportErrorInfo(ex.Message, ex.StackTrace, "AppDelegate-DidReceiveRemoteNotification");

        }
    }

点击推送通知后我需要再次打开闪屏。

这是App.cs代码

     public App(string openPageName, string openAppName,string notification)
    {  
            ServiceContainer.Resolve<IPushNotificationActionService>()
            .ActionTriggered += NotificationActionTriggered;
            InitMainPage(openPageName, openAppName, notification);
    } 

   private void InitMainPage(string pageName,string appName,string notification)
    {
        ServiceContainer.Resolve<IPushNotificationActionService>()
      .ActionTriggered += NotificationActionTriggered;
        PushNotificationActionService.PushNotificationPageName = pageName ;
        PushNotificationActionService.PushNotificationAppName = appName;
        PushNotificationActionService.PushNotificationMessage = notification;
        MainPage = new NavigationPage(new Splash(pageName));
     }

所有方法都在调用,推送通知数据也在 iOS 中正确加载。但不导航到 Spalsh Screen。任何人有解决此问题的想法请帮助。

如果应用是运行,我们不需要用LoadApplication重新加载应用,我们可以直接发送消息在App.cs中设置MainPage

iOS

if (UIApplication.SharedApplication.ApplicationState.Equals(UIApplicationState.Active))
{
     MessagingCenter.Send<object>(this,"Hi");
}

表格

public App()
{
    InitializeComponent();

    MainPage = new MainPage();

    MessagingCenter.Subscribe<object>(this, "Hi", (obj) => {
       MainPage = new NavigationPage(new Splash(pageName));
    });
}