温馨提示:本文翻译自stackoverflow.com,查看原文请点击:xamarin.forms - Xamarin Forms IOS with Azure Notification Hub
apple-push-notifications azure xamarin.forms xamarin.ios

xamarin.forms - Xamarin使用Azure Notification Hub形成IOS

发布于 2020-05-05 07:18:34

我知道这个问题被问过多次。所以在这里忍受我。

我正在尝试使用Xamarin Forms与My Android / iOS客户端一起实现Azure Notification Hub。

对于Android来说可以,但是对于iOS我没有收到任何通知。

这是我为iOS做的清单

  1. 生成证书和配置文件
  2. 在MacOS上注册证书
  3. 导出p12文件并导入Azure Hub
  4. 我还在Info.plist中添加了权限(后台远程通知)
  5. Entitlements.plist文件中没有任何更改
  6. 我可以从APN服务获取设备令牌
  7. 获得令牌后,我可以在Azure Hub上注册

但是我无法收到任何通知,并且没有调用任何功能(DidRegisterUserNotificationSettings,WillPresentNotification,ReceivedRemoteNotification,DidRegisterUserNotificationSettings,FailedToRegisterForRemoteNotifications,DidReceiveRemoteNotification)

但是RegisteredForRemoteNotifications被称为

我还测试了设备令牌,无论有没有将其删除'<> ',但都没有成功。

可能是什么问题?

这是我的代码

 public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            GlobalVariables.Instance.PnsToken = deviceToken.Description
                                 .Replace("<", string.Empty)
                                 .Replace(">", string.Empty)
                                 .Replace(" ", string.Empty)
                                 .ToUpper();
            RegisteredForHub(application, deviceToken);
        }

        public void RegisteredForHub(UIApplication application, NSData deviceToken)
        {
            string pnsHandle = deviceToken.Description
                                 .Replace("<", string.Empty)
                                 .Replace(">", string.Empty)
                                 .Replace(" ", string.Empty)
                                 .ToUpper();
            _hub = new SBNotificationHub(Constant.ListenConnectionString, Constant.NotificationHubName);

            _hub.UnregisterNative((error) =>
            {
                if (error != null)
                {
                    Debug.WriteLine($"Unable to call unregister Native {error}");
                }
            });
            _hub.UnregisterTemplate("defaultTemplate",(error) =>
            {
                if (error != null)
                {
                    Debug.WriteLine($"Unable to call unregister Template {error}");
                }
            });
            // update registration with Azure Notification Hub
            _hub.UnregisterAll(deviceToken, (error) =>
            {
                if (error != null)
                {
                    Debug.WriteLine($"Unable to call unregister {error}");
                    return;
                }

                var tags = new NSSet(Constant.SubscribeTags);
                _hub.RegisterNative(pnsHandle, tags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
                    }
                });
                var templateExpiration = DateTime.Now.AddDays(120)
                    .ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                //_hub.RegisterTemplate(deviceToken, "defaultTemplate", Constant.ApnTemplateBody, templateExpiration,
                //    tags, (errorCallback) =>
                //    {
                //        if (errorCallback != null)
                //        {
                //            Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                //        }
                //    });
            });

查看更多

提问者
Noob Coder
被浏览
18
Noob Coder 2020-02-24 18:07

经过大量的试验和错误以及数小时的谷歌搜索。我发现问题出在Azure上。

我已经上传了正确的证书,但是密码是一个字符,因此未发送任何通知。

如果用户为其证书文件输入了错误的密码,Azure应提示用户输入APN。