TsgcWSAPIServer_WebPush方法 › SendNotification

SendNotification 方法

使用已配置的 VAPID 凭据,对单个订阅端点加密并以 POST 方式发送 Web Push 通知。

重载

重载 1

语法

procedure SendNotification(const aSubscription : TsgcHTTP_API_WebPush_PushSubscription; const aMessage: TsgcWebPushMessage);

参数

名称类型描述
aSubscriptionconst TsgcHTTP_API_WebPush_PushSubscription目标订阅,包含推送服务端点以及浏览器用于加密载荷的 p256dh 公钥和 AuthSecret。
aMessageconst 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;

重载 2

语法

procedure SendNotification(const aSubscription : TsgcHTTP_API_WebPush_PushSubscription; const aMessage: string);

参数

名称类型描述
aSubscriptionconst TsgcHTTP_API_WebPush_PushSubscription目标订阅,其 Endpoint、PublicKey(p256dh)和 AuthSecret 驱动加密的 POST 请求。
aMessageconst 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"}}');

返回方法