Xamarin.iOS应用程序激活时不会接收parse.com推送

本文关键字:parse com 推送 iOS 应用程序 激活 Xamarin | 更新日期: 2023-09-27 18:27:20

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

但只有当应用程序处于非活动状态时,推送才会到达,例如在后台或关闭。如果该应用程序是活动应用程序,则不会出现推送。

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

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

即使应用程序处于活动状态,我也应该更改什么以使其接收推送并像正常情况一样对待它们?

此应用程序已发布,并可同时使用live&调试时。但是,当推送处于活动状态时没有到达是我的问题。

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

谢谢你抽出时间。

Xamarin.iOS应用程序激活时不会接收parse.com推送

这是解决方案。

内部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)
            {
            }
        }