如何访问推送通知中的参数

How to access parameters in Push Notification

我正在使用 mvvmCross 5.3 和 Xamarin 6.3,需要帮助才能访问通过通知传递的数据。

当我收到通知时,我收到了一个由 Apple 发送的标准 Jason 和一个我将用来指向应用程序中某个屏幕的参数。

在我的 AppDelegate 中,我有以下代码:

 public override bool FinishedLaunching(UIApplication application, NSDictionary options)
        {
            Window = new UIWindow(UIScreen.MainScreen.Bounds);

            var setup = new Setup(this, Window);
            setup.Initialize();

            var startup = Mvx.Resolve<IMvxAppStart>();
            startup.Start();

            Window.MakeKeyAndVisible();

            //Push Notifications
            if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
            {
                var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                                   UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                                   new NSSet());

                UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }
            else
            {
                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
            }

            return ApplicationDelegate.SharedInstance.FinishedLaunching(application, options);
        }

在参数选项中我知道我得到了我需要的信息,但我不知道如何在我的代码中访问这些信息。

----编辑

2017-10-26 11:39:28.680 App.IOs[6733:2676232] 
{
    UIApplicationLaunchOptionsRemoteNotificationKey =     
    {
        aps =         
        {
            alert =             
            {
                body = "Message";
                title = "Title";
            };
        };
        idOrder = 254;
    };
}

您可以创建这样的方法并在 iOS 方法中调用它,在 AppDelegate 中接收通知:

private void HandleReceivedNotification(NSDictionary userInfo = null)
{

    if (userInfo != null)
    {
        var apsDictionary = userInfo["aps"] as NSDictionary;
        var alertDictionary = apsDictionary["alert"] as NSDictionary;
        var body = alertDictionary["body"].ToString();
        var idOrder = userInfo["idOrder "].ToString();
    }
}

但不要忘记包含一个 try/catch 并检查是否有任何变量为空。