我正在尝试使用 Azure 通知中心为我的移动应用程序提供远程通知。
当我的移动应用程序首次安装在设备上时,我会在设备上创建并存储一个
installationId
,这是一个 GUID
值。我使用 Shiny 来处理 Apple/FCM 的设备注册,然后它给我一个 registrationToken
。然后,我将我的 installationId
和 registrationToken
以及设备平台传递到我的后端 API,在其中我使用下面的代码来处理注册:
public async Task RegisterDevice(Guid installationId, string registrationToken, string platform)
{
var installation = new Installation();
installation.InstallationId = installationId.ToString();
installation.PushChannel = registrationToken; // This is the registration token I get from Shiny
installation.Platform = platform == "ios" ? NotificationPlatform.Apns : NotificationPlatform.Fcm;
await _notificationHubsClient.CreateOrUpdateInstallationAsync(installation);
}
_notificationHubsClient
是我对 NotificationHubClient
的包装,它随 Microsoft.Azure.NotificationHubs
NuGet 包一起提供。
当我通过获取通知中心上的所有注册来检查注册信息时,我在
InstallationId
对象中的任何位置都看不到我的 JSON
。相反,我将 registrationToken
视为 InstallationId
下的 tags
。看起来像这样:
{
"eTag": "1",
"expirationTime": "9999-12-31T23:59:59.9999999Z",
"registrationId": "1234567890232290733-3250316115430032296-3",
"tags": [
"$InstallationId:{999a99a9999999a99a99a9a99aa99999}" // <- This is the registrationToken value
],
"pushVariables": null,
"pnsHandle": "aAaaBCdeF0qUgk7kMPrVUB...",
"isReadOnly": false,
"extensionData": {}
}
有人可以告诉我我在这里做错了什么吗?
好消息,你没有做错任何事。如果您使用读取安装操作检索安装,您的安装 ID 和令牌将与您在此处期望的格式匹配。
Azure 通知中心有两种“大部分”独立的方式来存储设备记录:安装和注册。大多数情况下,这种区别是一种允许 API 更改而不引入向后兼容性问题的方法。例如,更改设备句柄过期时发生的情况的策略、转向异步模型等。无论好坏,“读取所有注册”决定返回存储在中心的所有设备,而不仅仅是作为注册添加的记录。虽然这确实允许以统一的方式获取所有记录,但它确实会造成一些混乱。