通过 Azure 通知中心进行 Web 推送:JSON 值无法转换为 Microsoft.NotificationHubs.Contracts.LegacyModels.BrowserPushSubscription

问题描述 投票:0回答:1

我很难通过 Azure 通知中心进行 Web 推送通知。

浏览器通知支持已于 于 2024 年 3 月宣布,但看起来仍然缺少任何类型的适当可用性,尽管 thisthis 拉取请求等待几个月才能合并。

我花了相当多的时间尝试一些像这样的深奥建议,但我总是无法为基于浏览器的推送通知目标创建新的安装。

错误是:

Microsoft.Azure.NotificationHubs.Messaging.BadRequestException:  The provided request body is invalid. Error: The JSON value could not be converted to Microsoft.NotificationHubs.Contracts.LegacyModels.BrowserPushSubscription. Path: $.pushChannel | LineNumber: 0 | BytePositionInLine: 487.. TrackingId:dacca0ce-6af8-45b5-9ab5-ea1d1f78a57e,TimeStamp:9/21/2024 6:02:33 PM +00:00
   at Microsoft.Azure.NotificationHubs.NotificationHubClient.SendRequestAsync(HttpRequestMessage request, String trackingId, HttpStatusCode[] successfulResponseStatuses, CancellationToken cancellationToken)
   at Microsoft.Azure.NotificationHubs.NotificationHubClient.SendRequestAsync(HttpRequestMessage request, String trackingId, HttpStatusCode successfulResponseStatus, CancellationToken cancellationToken)
   at Microsoft.Azure.NotificationHubs.NotificationHubClient.<>c__DisplayClass91_0.<<CreateOrUpdateInstallationAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.Azure.NotificationHubs.NotificationHubRetryPolicy.RunOperation[T](Func`2 operation, CancellationToken cancellationToken)
   at Microsoft.Azure.NotificationHubs.NotificationHubRetryPolicy.RunOperation[T](Func`2 operation, CancellationToken cancellationToken)
   at Microsoft.Azure.NotificationHubs.NotificationHubClient.CreateOrUpdateInstallationAsync(Installation installation, CancellationToken cancellationToken)

通知中心 SDK 似乎正在将

BrowserPushSubscription
实例序列化为 JSON 字符串,然后将其保存到
PushChannel
属性中。

不幸的是,通知中心端点根本不喜欢它,因为它无法正确地反序列化它。

我尝试手动将其序列化为不同的格式(IE:表示浏览器订阅 JSON 的常见方法是将

P256DH
Auth
属性分组为
Keys
子属性),但没有成功。

有人让它工作吗? 谢谢

azure asp.net-core azure-notificationhub web-push
1个回答
0
投票

自行构建手动 JSON 负载并通过通知中心 REST API 发送,绕过 SDK。

JSON 有效负载:

{
  "installationId": "your-installation-id",
  "platform": "browser",
  "pushChannel": {
    "endpoint": "https://your-endpoint-url.com",
    "keys": {
      "p256dh": "your-p256dh-key",
      "auth": "your-auth-key"
    }
  },
  "tags": ["your-tag"]
}

直接使用REST API:

curl -X PUT \
  https://<your-notification-hub-namespace>.servicebus.windows.net/<your-hub-name>/installations/<your-installation-id>?api-version=2020-06 \
  -H "Content-Type: application/json" \
  -H "Authorization: SharedAccessSignature sr=<resource-uri>&sig=<signature>&se=<expiry>&skn=<policy-name>" \
  -d '{
    "installationId": "your-installation-id",
    "platform": "browser",
    "pushChannel": {
      "endpoint": "https://your-endpoint-url.com",
      "keys": {
        "p256dh": "your-p256dh-key",
        "auth": "your-auth-key"
      }
    },
    "tags": ["your-tag"]
  }'

生成共享访问签名(SAS),您可以使用Azure Portal或C#

中类似的脚本

代码:

string resourceUri = "<namespace>.servicebus.windows.net";
string keyName = "<your-key-name>";
string key = "<your-primary-or-secondary-key>";

TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
int expiry = Convert.ToInt32(sinceEpoch.TotalSeconds) + 3600; // 1 hour expiration

string stringToSign = WebUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

string sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
    WebUtility.UrlEncode(resourceUri), WebUtility.UrlEncode(signature), expiry, keyName);

Console.WriteLine(sasToken);

enter image description here

响应(成功):

HTTP/1.1 200 OK
Date: Tue, 24 Sep 2024 14:02:33 GMT
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
TrackingId: 3429bafa-212b-44ab-9b7e-caa3f217b7f5
x-ms-request-id: 7db24283-22c7-4e93-b865-9f6f063d5c7f
x-ms-version: 2020-02-09
© www.soinside.com 2019 - 2024. All rights reserved.