首页 > 解决方案 > 如何从远程推送通知中获取 APNS 有效负载?

问题描述

我正在我的 OSX 应用程序中添加远程推送通知支持。我可以通过 apns 在我的 PC 中成功接收推送通知,当我单击通知横幅时,这就是我看到的行为。

如果我的应用程序没有运行,它将启动应用程序。但永远不会点击“ReceivedRemoteNotification()”。这是预期的行为吗?在这种情况下有什么方法可以接收 apns 有效载荷?

如果我的应用程序正在运行,我会通过“ReceivedRemoteNotification”获取有效负载,并且一切正常。

所以如果我们的应用程序没有运行,我们就无法在我们的应用程序中获取 apns 有效负载?

任何帮助都非常感谢。

谢谢,吉塞什

标签: iosmacosapple-push-notifications

解决方案


弄清楚了。有效载荷可通过 DidFinishLaunching 的“NSNotification”参数获得。

 public override void DidFinishLaunching(NSNotification notification)
        {
            var uInfo = notification.UserInfo;
            if(uInfo["NSApplicationLaunchUserNotificationKey"] != null)
            {
                var payLoad = uInfo["NSApplicationLaunchUserNotificationKey"] as NSUserNotification;
                if(payLoad.ActivationType == NSUserNotificationActivationType.ActionButtonClicked)
                {
                    NSAlert alert = new NSAlert();
                    alert.MessageText = "Button Clicked";
                    alert.RunModal();
                }
                var userInfo = payLoad.UserInfo;

                //not.ActivationType == 
                if (userInfo != null && userInfo.ContainsKey(new NSString("aps")))
                {
                    //Get the aps dictionary
                    NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
                    if (aps.ContainsKey(new NSString("content")))
                    {
                        var deepLink = (aps[new NSString("content")] as NSString).ToString();
                        NSAlert alert = new NSAlert();
                        alert.MessageText = deepLink;
                        alert.RunModal();
                        //DisplaySubView(deepLink);
                        //Console.WriteLine($"Deeplink : {deepLink}");
                    }
                }

            }

推荐阅读