【问题标题】:Xamarin Forms iOS - Saving a user tag in Azure Notification Hubs works in AppDelegate but not in a serviceXamarin Forms iOS - 在 Azure 通知中心中保存用户标记适用于 AppDelegate 但不适用于服务
【发布时间】:2020-12-08 15:34:16
【问题描述】:

我目前正在尝试使用 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);

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

Registered for push notifications with token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

所以,我不知道如何在成功登录后注册 UserId 以及为什么它在一个地方有效而在另一个地方无效。

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

【问题讨论】:

标签: xamarin.forms xamarin.ios apple-push-notifications azure-notificationhub


【解决方案1】:

你可能和我和其他几个人遇到了同样的错误。

基本上 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 问题中的解决方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2020-06-01
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多