TsgcWebPush_Client方法 › SendNotification

SendNotification 方法

使用 aes128gcm 加密载荷,通过 VAPID 签名请求,并将其 POST 到浏览器订阅端点。

语法

procedure SendNotification(const aPushSubscription : TsgcHTTP_API_WebPush_PushSubscription; const aText: string);

参数

名称类型描述
aPushSubscriptionconst TsgcHTTP_API_WebPush_PushSubscription浏览器在注册时返回的订阅记录——携带 Endpoint(推送服务的 POST URL)、PublicKey(用户代理 P-256 公钥,p256dh,Base64URL 编码)和 AuthSecret(16 字节 auth 密钥,Base64URL 编码),用于推导每条消息的内容加密密钥。
aTextconst string要传递给浏览器的明文载荷(通常是由 Service Worker 解释的 JSON 文档)。该值采用 UTF-8 编码,并在发送前以 aes128gcm 加密;传入空字符串将发送无载荷的通知。

备注

单次 Web Push 发送(RFC 8030 + RFC 8291)。该方法验证 VAPID 密钥已配置,且 aPushSubscription 包含 Endpoint、PublicKey 和 AuthSecret;针对用户代理公钥计算 ECDH 共享密钥;用 HKDF 派生内容加密密钥和随机数;用 aes128gcm 加密 aText;构建用 PEM 私钥签名的 VAPID JWT(ES256)并将其作为 Authorization 标头添加;将 DER 公钥放入 Crypto-Key 标头;并将二进制密文 POST 到订阅端点。缺少密钥将引发 EsgcWebSocketException;推送服务成功时返回 201/202,订阅已过期且必须丢弃时返回 404/410,应用限流时返回 429。

示例

var
  oSubscription: TsgcHTTP_API_WebPush_PushSubscription;
begin
  oSubscription := TsgcHTTP_API_WebPush_PushSubscription.Create;
  try
    oSubscription.Endpoint := 'https://fcm.googleapis.com/fcm/send/...';
    oSubscription.PublicKey := 'BASE64URL_P256DH';
    oSubscription.AuthSecret := 'BASE64URL_AUTH';

    sgcWebPush_Client1.VAPID.PEM.PrivateKey.Text := '-----BEGIN PRIVATE KEY-----...';
    sgcWebPush_Client1.VAPID.DER.PrivateKey := 'BASE64URL_PRIVATE_KEY';
    sgcWebPush_Client1.VAPID.DER.PublicKey := 'BASE64URL_PUBLIC_KEY';
    sgcWebPush_Client1.VAPID.Details.MailTo := 'info@esegece.com';

    sgcWebPush_Client1.SendNotification(oSubscription,
      '{"title":"eSeGeCe","body":"Hello from sgcWebSockets!"}');
  finally
    oSubscription.Free;
  end;
end;

返回方法