微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS推送通知未从Azure接收到物理设备

如何解决iOS推送通知未从Azure接收到物理设备

我已经创建了一个示例应用程序,用于在iOS上测试推送通知。我在PC上运行Visual Studio 2019,该PC通过本地网络连接到MAC。 iPhone通过避雷线连接到MAC。顾名思义,当我测试从Azure发送时,永远不会调用ReceivedRemoteNotification方法。 Azure通知我测试消息已成功发送。

我已经多次严格遵循本指南:

https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started

RegisteredForRemoteNotifications被调用,因此该应用似乎成功注册了推送通知。我还会在我接受的物理设备上获得许可请求。但是我注意到,当我将鼠标悬停在此方法内的“ Hub”上时,在类句柄下会出现错误

“无法将类型为“ Mono.Debugger.soft.PointerValue”的对象强制转换为类型为“ Mono.Debugger.soft.PrimitiveValue”。”

但是我不确定这是否真的与我的问题有关。除了此链接之外,我找不到任何在线内容,这仅表明针对该错误的一种解决方法是在发行版中运行而不是调试。

Xamarin.Forms Unable to cast object of type 'System.RuntimeType' to type 'Mono.Debugger.Soft.TypeMirror' when trying to create MasterDetailPage

直接从上面链接的Microsoft指南中获取示例代码

namespace PushNotifTestApp.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application,as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        private SBNotificationHub Hub { get; set; }

        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window,load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method,or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app,NSDictionary options)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(10,0))
            {
                UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationoptions.Alert | UNAuthorizationoptions.Badge | UNAuthorizationoptions.sound,(granted,error) => InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications));
            }
            else if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
            {
                var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                        UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.sound,new NSSet());

                UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }
            else
            {
                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
            }


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

            return base.FinishedLaunching(app,options);
        }

        public override void FailedToRegisterForRemoteNotifications(UIApplication application,NSError error)
        {
            
        }

        public override void RegisteredForRemoteNotifications(UIApplication application,NSData devicetoken)
        {
            Hub = new SBNotificationHub(Constants.ListenConnectionString,Constants.NotificationHubName);

            Hub.Unregisterall(devicetoken,(error) => {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}",error.ToString());
                    return;
                }

                NSSet tags = null; // create tags if you want
                Hub.RegisterNative(devicetoken,tags,(errorCallback) => {
                    if (errorCallback != null)
                        System.Diagnostics.Debug.WriteLine("RegisterNative error: " + errorCallback.ToString());
                });
            });
        }

        public override void ReceivedRemoteNotification(UIApplication application,NSDictionary userInfo)
        {
            ProcessNotification(userInfo,false);
        }

        void ProcessNotification(NSDictionary options,bool fromFinishedLaunching)
        {
            // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
            if (null != options && options.ContainsKey(new Nsstring("aps")))
            {
                //Get the aps dictionary
                NSDictionary aps = options.ObjectForKey(new Nsstring("aps")) as NSDictionary;

                string alert = string.Empty;

                //Extract the alert text
                // NOTE: If you're using the simple alert by just specifying
                // "  aps:{alert:"alert msg here"}  ",this will work fine.
                // But if you're using a complex alert with Localization keys,etc.,// your "alert" object from the aps dictionary will be another NSDictionary.
                // Basically the JSON gets dumped right into a NSDictionary,// so keep that in mind.
                if (aps.ContainsKey(new Nsstring("alert")))
                    alert = (aps[new Nsstring("alert")] as Nsstring).ToString();

                //If this came from the ReceivedRemoteNotification while the app was running,// we of course need to manually process things like the sound,badge,and alert.
                if (!fromFinishedLaunching)
                {
                    //Manually show an alert
                    if (!string.IsNullOrEmpty(alert))
                    {
                        var myAlert = UIAlertController.Create("Notification",alert,UIAlertControllerStyle.Alert);
                        myAlert.AddAction(UIAlertAction.Create("OK",UIAlertActionStyle.Default,null));
                        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(myAlert,true,null);
                    }
                }
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。