问题描述
我目前正在尝试使用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,(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
作为解决方法,您可以使用不涉及回调(而不是返回错误消息)或上述95问题中的解决方法的SBNotificationHub方法重载。