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

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

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

当我收到通知时,我收到的是 Apple 发送的标准 JSON。我还收到了一个参数,我将使用该参数指向我的应用程序中的某个屏幕。

在我的 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;
    };
}

最佳答案

您可以创建这样的方法并在 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 并检查是否有任何变量为空。

关于ios - 如何访问推送通知中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46957374/

相关文章:

ios - WatchOS2 WCSession sendMessage 不会在后台唤醒 iPhone

ios - 当应用程序 rootViewController 是 tabBarController 时 UINavigationController 的奇怪行为

ios - 推送通知 : no sound

iOS Swift GCM/FCM 在后台接收通知

ios - AVFoundation.AVAudioPlayer 随机停止

ios - 在 Swift 中过滤自定义对象数组

ios - 过渡动画中的闪烁(模拟器?)

ios - 适用于 iOS 的 GCM 下游消息传递 JSON 格式

ios - iOS 7 中 MonoTouch.Dialog 支持拉动刷新

xaml - Xamarin Forms Center Collection查看内容