azure - 我可以通过设备令牌获取注册吗?
问题描述
是否可以在不下载所有注册的情况下通过设备令牌找到设备?我应该将设备令牌添加为标签并根据它进行查找吗?
如果在同一设备上与新用户添加了新注册,我需要删除设备的先前注册。
解决方案
我将设备令牌添加为标签。Android 令牌需要两个标签,因为它们的长度超过了标签的最大长度。
public static string FormatDeviceTokenTag(int index, string token) => $"DEVICE_TOKEN{index}:{token}";
public static IList<string> GetDeviceTokenTags(string token)
{
const int MaxTagLength = 120;
var tag0 = FormatDeviceTokenTag(0, token);
var tag0Clipped = tag0.Substring(0, Math.Min(tag0.Length, MaxTagLength));
var tags = new List<string> { tag0Clipped };
if (tag0.Length > MaxTagLength)
{
var tag1 = FormatDeviceTokenTag(1, tag0.Substring(MaxTagLength));
if (tag1.Length > MaxTagLength)
{
throw new ArgumentException("Token too long");
}
tags.Add(tag1);
}
return tags;
}
然后找到匹配的令牌...
async Task<IList<RegistrationDescription>> GetExistingRegistrations(string token, EnumMobileDeviceType deviceType)
{
var tags = GetDeviceTokenTags(token);
var registrations = await CreateClient().GetRegistrationsByTagAsync(tags.First(), 10000);
var registrations2 = registrations.Where(r => tags.Count > 1 ?
r.Tags.Contains(tags[1]) :
!r.Tags.Any(t =>
t.StartsWith(FormatDeviceTokenTag(1, string.Empty), StringComparison.Ordinal)));
registrations2 = registrations2.Where(r =>
(deviceType == EnumMobileDeviceType.Apple && r is AppleTemplateRegistrationDescription) ||
(deviceType == EnumMobileDeviceType.Droid && r is GcmTemplateRegistrationDescription));
return registrations2.ToList();
}
多么繁琐的工作啊。我错过了 API 中的某些内容吗?
推荐阅读
- javascript - 在 JavaScript 中按类单击切换切换功能
- mysql - 超出 EF Core 异步存储过程锁定等待超时
- java - 已经设置了资源文件夹。但是 getclass().get resource().getPath() 在 Intellj IDEA Java 中仍然失败
- tensorflow - Tensorflow Lite 和代表性数据集
- tkinter - 如何在 tkinter 中创建组合框数组?
- if-statement - 如果:我做错了什么
- php - PHP将缺少的工作日推入数组中,总数为0
- google-apps-script - 以 Google 群组的身份登录以为应用脚本创建可安装触发器
- c# - 多次调用 AddHostedServices 不会创建多个实例
- html - 使用primeng和angular从选定项目中获取p-dropdown选项值