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

时间:2021-02-22 16:27:36

I can send pushes to my users both through parse.com and through their API from my website.

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

But the pushes ONLY arrive if the app is inactive, e.g either in the background or turned/killed off. If the app is the active app then the push does not appear.

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

I want the push to arrive like it does outside the app(e.g a notification that appears at the top and appears in the notification log in iOS)

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

My code is made in Xamarin.iOS and uses practically the default parse.com Xamarin.iOS pre-made template.

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

What is it that I should change to make it receive pushes and treat them like normally even while the app is active?

即使在应用程序处于活动状态时,我应该改变什么才能使其接受推送并正常对待它们?

This app has been published and works both live & while debugging. But the pushes not arriving when active is my issue here.

此应用程序已发布,可在现场和调试时使用。但是,活跃的推动不是我的问题。

        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);
        }

Thank you for your time.

感谢您的时间。

1 个解决方案

#1


0  

Here is the solution.

这是解决方案。

Inside ParsePush.ParsePushNotificationReceived that can be seen above

在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);

Also need to add this function This is also in AppDelegate

还需要添加此功能这也是在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)
            {
            }
        }

#1


0  

Here is the solution.

这是解决方案。

Inside ParsePush.ParsePushNotificationReceived that can be seen above

在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);

Also need to add this function This is also in AppDelegate

还需要添加此功能这也是在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)
            {
            }
        }