如何从远程推送通知获取APNS负载?

时间:2019-07-19 21:59:41

标签: ios macos apple-push-notifications

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

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

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

因此,如果应用未运行,我们是否无法在应用中获取apns有效负载?

任何帮助都很感激。

谢谢, 吉特什

1 个答案:

答案 0 :(得分:0)

弄清楚了。有效负载可通过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}");
                    }
                }

            }