TsgcWSAPIServer_WebPush › 方法 › SendNotification
使用已配置的 VAPID 凭据,对单个订阅端点加密并以 POST 方式发送 Web Push 通知。
procedure SendNotification(const aSubscription : TsgcHTTP_API_WebPush_PushSubscription; const aMessage: TsgcWebPushMessage);
| 名称 | 类型 | 描述 |
|---|---|---|
aSubscription | const TsgcHTTP_API_WebPush_PushSubscription | 目标订阅,包含推送服务端点以及浏览器用于加密载荷的 p256dh 公钥和 AuthSecret。 |
aMessage | const TsgcWebPushMessage | 包含 Title、Body、Icon 和 Url 的结构化消息;在加密之前通过 AsJSON 序列化为 JSON。 |
当您希望库为浏览器的 Service Worker 构建格式正确的通知负载时,请使用此首选重载。消息的 AsJSON 表示使用订阅密钥通过 aes128gcm 加密,并按照 RFC 8030 POST 到推送服务。
var
oMessage: TsgcWebPushMessage;
begin
oMessage := TsgcWebPushMessage.Create;
try
oMessage.Title := 'eSeGeCe Notification';
oMessage.Body := 'Subscription Successfully Registered!!!';
oMessage.Icon := 'https://www.esegece.com/images/esegece_logo_small.png';
oMessage.Url := 'https://www.esegece.com';
sgcWSAPIServer_WebPush1.SendNotification(aSubscription, oMessage);
finally
oMessage.Free;
end;
end;
procedure SendNotification(const aSubscription : TsgcHTTP_API_WebPush_PushSubscription; const aMessage: string);
| 名称 | 类型 | 描述 |
|---|---|---|
aSubscription | const TsgcHTTP_API_WebPush_PushSubscription | 目标订阅,其 Endpoint、PublicKey(p256dh)和 AuthSecret 驱动加密的 POST 请求。 |
aMessage | const string | 加密后原样发送的原始负载;通常是您的 service worker 知道如何解析的 JSON 字符串。 |
当您已持有预构建的 JSON 或纯文本有效载荷(例如由 TsgcJSON 生成或从存储加载)时,请使用此重载。推送服务通常接受最多约 4 KB 的加密内容;超出此限制会触发 413 响应,该响应通过 OnWebPushSendNotificationException 重新抛出。
sgcWSAPIServer_WebPush1.SendNotification(aSubscription,
'{"title":"Hello","body":"Raw JSON payload","data":{"url":"https://www.esegece.com"}}');