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

时间:2017-10-26 14:45:32

标签: ios push-notification xamarin.ios apple-push-notifications

我正在使用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;
    };
}

1 个答案:

答案 0 :(得分:1)

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

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并检查是否有任何变量为空。