TsgcWebPush_ClientMethods › SendNotification

SendNotification Method

Encrypts the payload with aes128gcm, VAPID-signs the request and POSTs it to the browser subscription endpoint.

Syntax

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

Parameters

NameTypeDescription
aPushSubscriptionconst TsgcHTTP_API_WebPush_PushSubscriptionSubscription record returned by the browser at registration time — carries Endpoint (the push service URL to POST to), PublicKey (user-agent P-256 public key, p256dh, Base64URL-encoded) and AuthSecret (16-byte auth secret, Base64URL-encoded) used to derive the per-message content encryption key.
aTextconst stringClear-text payload to deliver to the browser (typically a JSON document interpreted by the Service Worker). The value is UTF-8 encoded and encrypted with aes128gcm before being sent; an empty string sends a notification with no payload.

Remarks

Single-call Web Push send (RFC 8030 + RFC 8291). The method validates that VAPID keys are configured and that aPushSubscription contains an Endpoint, PublicKey and AuthSecret; computes an ECDH shared secret against the user-agent public key; derives the content encryption key and nonce with HKDF; encrypts aText with aes128gcm; builds a VAPID JWT (ES256) signed with the PEM private key and adds it as the Authorization header; places the DER public key in the Crypto-Key header; and POSTs the binary ciphertext to the subscription endpoint. Missing keys raise an EsgcWebSocketException; the push service returns 201/202 on success, 404/410 when the subscription has expired and must be dropped, or 429 when throttling is applied.

Example

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;

Back to Methods