首页 > 解决方案 > Xamarin Forms iOS - 在 Azure 通知中心中保存用户标记在 AppDelegate 中有效,但在服务中无效

问题描述

我目前正在尝试使用 Azure 通知中心让推送通知适用于我的移动应用程序。Android 运行良好,在 AppDelegate 中设置的初始 iOS 使用示例标签可以正常工作。

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            if (deviceToken == null)
            {
                return;
            }
            
            SBNotificationHub hub = new SBNotificationHub(CommonConstants.LISTEN_CONNECTION_STRING, CommonConstants.NOTIFICATION_HUB_NAME);

            // update registration with Azure Notification Hub
            hub.UnregisterAll(deviceToken, async (error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to call unregister {error}");
                    return;
                }

                string[] tags = new[] { "iostestpush" };
                NSSet userTags = new NSSet(tags);
                hub.RegisterNative(deviceToken, userTags, (error) =>
                {
                    if (error != null)
                    {
                        System.Diagnostics.Debug.WriteLine($"Unable to call register {error}");
                        return;
                    }
                });

                var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                hub.RegisterTemplate(deviceToken, "defaultTemplate", CommonConstants.APN_TEMPLATE_BODY, templateExpiration, userTags, (errorCallback) =>
                {
                    if (errorCallback != null)
                    {
                        System.Diagnostics.Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
                    }

                });
            });
        }

我遇到的问题是我需要在成功登录后注册 UserId。所以我用上面的代码设置了一个服务,将令牌作为字符串保存到设备中,这样就可以在服务中检索它并转换回 NSData 令牌

NSData deviceToken = new NSData(token, NSDataBase64DecodingOptions.None);

成功登录后,我将令牌字符串和标签数组发送到我的服务。

string[] userTag = new[] { loginResponse.UserId.ToString() };
await this._azureReg.SendRegistrationToServer(deviceToken, userTag);

除了将令牌转换回 NSData 并将用户标签转换为 NSSet 之外,除了名称更改之外,它与上述相同。但是 Azure 声称没有注册,即使我的输出显示

Registered for push notifications with token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

我以为是字符串来回转换,所以在 AppDelegate 中进行了测试,效果很好。

因此,我不知道如何在成功登录后注册 UserId 以及为什么它在一个地方工作而在另一个地方工作。

我希望这很清楚,并提前感谢您的任何建议。

标签: xamarin.formsxamarin.iosapple-push-notificationsazure-notificationhub

解决方案


您可能遇到了与我和其他几个相同的错误。

基本上 SBNotificationHub 方法重载,如带有回调签名的 UnregisterAll 和 RegisterTemplate,当您在主线程之外使用它们时,使用迄今为止的库。我也出于同样的目的使用了一个服务(处理具有不同标签的跨平台推送,特别是对于用户 ID),但我的实现涉及为此关闭主线程。

我们记录并正在解决的错误在这里:https ://github.com/Azure/azure-notificationhubs-ios/issues/95

目前的解决方案是完全放弃 SBNotificationHub。Xamarin / Azure 文档已过时,SBNOtificationHub 是旧代码。推荐的库是 MSNotificationHub。https://github.com/azure/azure-notificationhubs-xamarin

作为解决方法,您可以使用不涉及回调的 SBNotificationHub 方法重载(它们会返回错误消息)或上述 95 问题中的解决方法。


推荐阅读