【发布时间】: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 以及为什么它在一个地方有效而在另一个地方无效。
我希望这很清楚,并提前感谢您的任何建议。
【问题讨论】:
-
问题:您的后端
this._azureReg.SendRegistrationToServer是做什么的?它是否要求更新注册?附带说明一下,您可以尝试使用MSNotificationHub使用安装而不是注册的新 API 吗?在这里我们谈谈Registrations vs Installations。
标签: xamarin.forms xamarin.ios apple-push-notifications azure-notificationhub