Xamarin.iOS应用程序在激活时不会收到parse.com PUSH

时间:2015-12-18 09:14:30

标签: c# ios parse-platform xamarin xamarin.ios

我可以通过parse.com和我网站上的API向我的用户发送推送信息。

但是,如果应用程序处于非活动状态,则推送仅会到达,例如在后台或转动/终止。 如果该应用是有效应用,则不会显示推送。

我希望推送能够像应用程序外部一样到达(例如,顶部显示的通知并显示在iOS的通知日志中)

我的代码是在Xamarin.iOS中制作的,几乎使用了默认的parse.com Xamarin.iOS预制模板。

即使应用程序处于活动状态,我应该更改它以使其接受推送并正常对待它们是什么?

此应用已发布,可在线直播在调试时。但是,活跃的推动不是我的问题。

        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            // Initialize the Parse client with your Application ID and .NET Key found on
            // your Parse dashboard
            ParseClient.Initialize("key1", "key2");

            // Register for Push Notitications
            UIUserNotificationType notificationTypes = (UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound);
            var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet(new string[] { }));
            UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications();

            // Handle Push Notifications
            ParsePush.ParsePushNotificationReceived += (object sender, ParsePushNotificationEventArgs args) => {
                // Process Push Notification payload here.
            };

            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }

        public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings)
        {
            application.RegisterForRemoteNotifications();
        }

        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            ParseObject obj = ParseObject.Create("_Installation");

            string dt = deviceToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
            obj["deviceToken"] = dt;
            obj.SaveAsync().ContinueWith(t => {
                if (t.IsFaulted)
                {
                    using (IEnumerator<System.Exception> enumerator = t.Exception.InnerExceptions.GetEnumerator())
                    {
                        if (enumerator.MoveNext())
                        {
                            ParseException error = (ParseException)enumerator.Current;
                        }
                    }
                }
                else
                {
                    var data = NSUserDefaults.StandardUserDefaults;
                    data.SetString("currentInstallation", obj.ObjectId);
                }
            });

            parseDeviceInfo.deviceToken = dt;

            //Original parse.com code that does not work
            //ParseInstallation installation = ParseInstallation.CurrentInstallation;
            //installation.SetDeviceTokenFromData(deviceToken);

            //installation.SaveAsync();
        }

        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
            // We need this to fire userInfo into ParsePushNotificationReceived.
            ParsePush.HandlePush(userInfo);
        }

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

这是解决方案。

在ParsePush.ParsePushNotificationReceived里面,可以在上面看到

// Process Push Notification payload here.

                string message = "";

                try
                {

                    var payload = args.Payload;
                    object aps;
                    if (payload.TryGetValue("aps", out aps))
                    {
                        string payloadStr = "";

                        try
                        {
                            payloadStr = aps.ToString();
                        }
                        catch (Exception e)
                        {
                        }

                        try
                        {
                            var match = Regex.Match(payloadStr, @"alert = (.*);\n");
                            if (match.Success)
                            {
                                string alertText = match.Groups[1].Value;
                                message = alertText;
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                catch (Exception e)
                {
                    message = "payload crash";
                }

                if (string.IsNullOrEmpty(message))
                    message = "";

                UILocalNotification notification = new UILocalNotification();
                //NSDate.FromTimeIntervalSinceNow(15);
                notification.FireDate = NSDate.FromTimeIntervalSinceNow(5);
                //notification.AlertTitle = "Alert Title"; // required for Apple Watch notifications
                notification.AlertAction = "Message";
                notification.AlertBody = message;
                notification.SoundName = UILocalNotification.DefaultSoundName;
                UIApplication.SharedApplication.ScheduleLocalNotification(notification);

还需要添加此功能 这也是在AppDelegate

public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
        {
            try
            {
                // show an alert
                UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
                okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

                //viewController.PresentViewController(okayAlertController, true, null);

                UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okayAlertController, true, null);

                // reset our badge
                UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
            }
            catch (Exception)
            {
            }
        }