xamarin.forms - 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 以及为什么它在一个地方工作而在另一个地方工作。
我希望这很清楚,并提前感谢您的任何建议。
解决方案
您可能遇到了与我和其他几个相同的错误。
基本上 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 问题中的解决方法。
推荐阅读
- html - 在移动视图的表格行中换行长文本
- android - 如何使用谷歌登录制作apk?
- python - 如何将 UTC 日期时间转换为输出格式中的 T 和 Z?
- python - Python 将布尔变量输出到文件(读取、检查、写入)
- botframework - 瀑布步骤中的建议操作
- selenium - selenide.open 尝试抛出 NoSuchMethodError
- json - 尝试解码 json 时出错:simplejson.errors.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
- apache-kafka - 有没有办法为每个 kafka 主题获取所有制作人的 ip?
- python - 如何添加网格图作为一个图表的背景?
- django - 如何在 django 和 djangorestframework 中对创建 API 进行自定义验证?