WebPush
TsgcHTMLWebPush: 실행 중이 아니거나 사용자의 페이지에 있지 않은 브라우저를 위한 Web Push로, Delphi, C++ Builder 및 .NET에서 사용할 수 있습니다. Enterprise 및 All-Access 에디션에서만 사용할 수 있습니다.
TsgcHTMLWebPush: 실행 중이 아니거나 사용자의 페이지에 있지 않은 브라우저를 위한 Web Push로, Delphi, C++ Builder 및 .NET에서 사용할 수 있습니다. Enterprise 및 All-Access 에디션에서만 사용할 수 있습니다.
Web Push는 브라우저가 닫혀 있거나 다른 페이지를 보고 있는 사용자에게도 도달합니다. 페이지가 구독하고, 애플리케이션이 세 개의 이벤트를 통해 구독 정보를 저장하며, Send를 한 번 호출하면 알림이 전달됩니다. Enterprise 및 All-Access 에디션에서만 사용할 수 있습니다.
TsgcHTMLWebPush(유닛 sgcHTML_WebPush)
마크업 없음: 브라우저로 전송되는 암호화된 푸시 메시지
Delphi, C++ Builder, .NET
VAPID 키 쌍을 한 번 생성하여 보관합니다. 키 쌍과 Subject를 지정하고, 저장소 이벤트를 처리하고, 엔진에 WebPush를, 그 템플릿에 WebPushEnabled를 설정한 다음 Send를 호출합니다.
// Enterprise and All-Access only: compiled when SGC_WEBPUSH is defined
uses
Data.DB, sgcHTML_WebPush, sgcHTMX_Engine_Server;
var
vPublic, vPrivate: string;
begin
// once, then keep both values in your own settings
TsgcHTMLWebPush.GenerateVAPIDKeys(vPublic, vPrivate);
FPush := TsgcHTMLWebPush.Create(Self);
FPush.VAPIDPublicKey := vPublic;
FPush.VAPIDPrivateKey := vPrivate;
FPush.Subject := 'mailto:support@example.com';
FPush.OnSaveSubscription := PushSave;
FPush.OnLoadSubscriptions := PushLoad;
FPush.OnDeleteSubscription := PushDelete;
FPush.OnSubscriptionExpired := PushExpired;
FPush.Enabled := True;
// oHTMX is a TsgcHTMX_Engine_Server
oHTMX.WebPush := FPush;
oHTMX.Template.WebPushEnabled := True;
// later, from anywhere in the application
FPush.Send('u1', 'Order shipped',
'Order 1042 left the warehouse', '/orders/1042');
end;
// the application owns the storage
procedure TForm1.PushSave(Sender: TObject;
const aUserID, aEndpoint, aP256dh, aAuth: string);
begin
SaveSubscription(aUserID, aEndpoint, aP256dh, aAuth);
end;
procedure TForm1.PushLoad(Sender: TObject; const aUserID: string;
const aList: TsgcHTMLWebPushSubscriptions);
var
oQuery: TDataSet;
begin
oQuery := OpenSubscriptions(aUserID);
try
// one Add per stored subscription of this user
while not oQuery.Eof do
begin
aList.Add(oQuery.FieldByName('endpoint').AsString,
oQuery.FieldByName('p256dh').AsString,
oQuery.FieldByName('auth').AsString);
oQuery.Next;
end;
finally
oQuery.Free;
end;
end;
procedure TForm1.PushDelete(Sender: TObject; const aUserID, aEndpoint: string);
begin
DeleteSubscription(aUserID, aEndpoint);
end;
procedure TForm1.PushExpired(Sender: TObject;
const aUserID, aEndpoint: string; aStatusCode: Integer);
begin
// 404 or 410: the push service dropped it for good
DeleteSubscription(aUserID, aEndpoint);
end;
// in the page, a click starts the permission prompt:
// <button data-sgc-webpush>Enable notifications</button>
// includes: sgcHTML_WebPush.hpp, sgcHTMX_Engine_Server.hpp
// Enterprise and All-Access only: compiled when SGC_WEBPUSH is defined
String vPublic, vPrivate;
// once, then keep both values in your own settings
TsgcHTMLWebPush::GenerateVAPIDKeys(vPublic, vPrivate);
FPush = new TsgcHTMLWebPush(this);
FPush->VAPIDPublicKey = vPublic;
FPush->VAPIDPrivateKey = vPrivate;
FPush->Subject = "mailto:support@example.com";
FPush->OnSaveSubscription = PushSave;
FPush->OnLoadSubscriptions = PushLoad;
FPush->OnDeleteSubscription = PushDelete;
FPush->OnSubscriptionExpired = PushExpired;
FPush->Enabled = true;
// oHTMX is a TsgcHTMX_Engine_Server
oHTMX->WebPush = FPush;
oHTMX->Template->WebPushEnabled = true;
// later, from anywhere in the application
FPush->Send("u1", "Order shipped",
"Order 1042 left the warehouse", "/orders/1042");
// the application owns the storage
void __fastcall TForm1::PushSave(TObject *Sender,
const UnicodeString aUserID, const UnicodeString aEndpoint,
const UnicodeString aP256dh, const UnicodeString aAuth)
{
SaveSubscription(aUserID, aEndpoint, aP256dh, aAuth);
}
void __fastcall TForm1::PushLoad(TObject *Sender, const UnicodeString aUserID,
TsgcHTMLWebPushSubscriptions *const aList)
{
// one Add per stored subscription of this user
for (const TStoredSubscription &row : LoadSubscriptions(aUserID))
aList->Add(row.Endpoint, row.P256dh, row.Auth);
}
// in the page, a click starts the permission prompt:
// <button data-sgc-webpush>Enable notifications</button>
using esegece.sgcWebSockets;
// once, then keep both values in your own settings
TsgcHTMLWebPush.GenerateVAPIDKeys(out var pub, out var priv);
var push = new TsgcHTMLWebPush();
push.VAPIDPublicKey = pub;
push.VAPIDPrivateKey = priv;
push.Subject = "mailto:support@example.com";
// the application owns the storage
push.OnSaveSubscription += (sender, aUserID, aEndpoint, aP256dh, aAuth) =>
SaveSubscription(aUserID, aEndpoint, aP256dh, aAuth);
push.OnLoadSubscriptions += (sender, aUserID, aList) =>
{
// one Add per stored subscription of this user
foreach (var row in LoadSubscriptions(aUserID))
aList.Add(row.Endpoint, row.P256dh, row.Auth);
};
push.OnDeleteSubscription += (sender, aUserID, aEndpoint) =>
DeleteSubscription(aUserID, aEndpoint);
push.OnSubscriptionExpired += (sender, aUserID, aEndpoint, aStatusCode) =>
DeleteSubscription(aUserID, aEndpoint); // 404 or 410: gone for good
push.Enabled = true;
// htmx is a TsgcHTMX_Engine_Server
htmx.WebPush = push;
htmx.Template.WebPushEnabled = true;
// later, from anywhere in the application
int delivered = push.Send("u1", "Order shipped",
"Order 1042 left the warehouse", "/orders/1042");
// in the page, a click starts the permission prompt:
// <button data-sgc-webpush>Enable notifications</button>
가장 자주 사용하게 되는 멤버.
Web Push는 Enterprise 및 All-Access 에디션에서만 사용할 수 있습니다. 이 유닛은 SGC_WEBPUSH가 정의된 경우에 컴파일되며, sgcVer.inc가 이 두 에디션에 대해 이를 설정합니다. 별도로 판매되는 sgcHTML 자체도 필요합니다. 알림 수신함과 환경설정 테이블은 sgcHTML의 일부이며 이 기능이 필요하지 않고, 필요한 것은 푸시 채널뿐입니다.
GenerateVAPIDKeys는 새 키 쌍을 반환하는 클래스 메서드입니다. 공개 키는 브라우저가 구독할 때 사용하는 base64url 애플리케이션 서버 키이고, 비공개 키는 PEM 형식의 EC 키입니다. 이를 VAPIDPublicKey와 VAPIDPrivateKey에 지정합니다. Subject는 VAPID 연락처로, mailto: 또는 https: URI입니다. 표준 Delphi 빌드는 암호화를 위해 OpenSSL 3.x를 로드하며 해당 API를 스스로 선택합니다. .NET 포트는 관리형 암호화를 사용하므로 OpenSSL이 필요하지 않습니다.
컴포넌트는 구독 정보를 자체적으로 보관하지 않습니다. OnSaveSubscription(aUserID, aEndpoint, aP256dh, aAuth)가 하나를 저장하고, OnLoadSubscriptions(aUserID, aList)는 aList를 사용자가 저장한 구독마다 Add(endpoint, p256dh, auth) 하나씩으로 채우며, OnDeleteSubscription(aUserID, aEndpoint)는 하나를 제거합니다. 각 항목은 TsgcHTMLWebPushSubscription입니다.
Send(aUserID, aTitle, aBody, aURL, aIcon, aTag, aTTL, aUrgency)는 사용자가 저장한 구독마다 알림 하나를 암호화하고(RFC 8291) VAPID 서명(RFC 8292)과 함께 푸시 서비스에 POST합니다. 전달한 구독의 수를 반환하며, Enabled가 False이면 0을 반환합니다. 전송을 담당하는 TsgcHTMLWebPushClient는 TsgcHTTP_API_WebPush_Client를 재사용합니다.
TTL의 기본값은 2419200초(28일)이며 Urgency는 비어 있어 긴급도 헤더를 보내지 않습니다. aTTL 또는 aUrgency를 Send에 전달하면 알림 하나에 대해 둘 중 하나를 재정의할 수 있습니다.
푸시 서비스가 404 또는 410으로 응답하면 OnSubscriptionExpired(aUserID, aEndpoint, aStatusCode)가 발생하므로 해당 구독을 삭제할 수 있습니다. 그 밖의 실패는 OnSendError(aUserID, aEndpoint, E)를 발생시키며, 나머지 구독은 계속 시도됩니다.
컴포넌트를 엔진에 지정하면 SubscribeEndpoint(/push/subscribe) 또는 UnsubscribeEndpoint(/push/unsubscribe)로 오는 POST에 자동으로 응답합니다. 처리되면 204, 잘못된 구독이면 400, 사용자를 확인할 수 없으면 403, 64 KB를 초과하면 413입니다. POST를 직접 라우팅하려면 HandleSubscribe(aJSON, aUserID)와 HandleUnsubscribe를 직접 호출하십시오.
사용자는 브라우저가 게시한 내용에서 읽지 않습니다. 엔진에 TsgcHTMLAuth가 있으면 세션 쿠키의 사용자이며 CSRF 토큰이 필요합니다. 없으면 OnResolveUser(aCookieHeader, aHeaders, aBody, aUserID, aAllowed)가 직접 정한 사용자 ID로 응답하거나 거부합니다. 둘 다 없으면 요청은 403을 받습니다.
Template.WebPushEnabled가 켜져 있으면 엔진은 공개 키와 엔드포인트를 템플릿에 복사하고, 서비스 워커를 ServiceWorkerPath에서 제공하며, 페이지에는 구독 스크립트가 포함됩니다. 권한 요청이 클릭에서 실행되도록 페이지에 <button data-sgc-webpush>를 넣으십시오. data-sgc-webpush="unsubscribe"는 이를 끕니다. 브라우저에는 https가 필요하며, 개발 중에는 localhost도 됩니다.
TsgcHTMLComponent_Notification이 둘 중에서 선택합니다. Presence와 WebPush를 지정하고, OnGetChannel에 ncPush로 응답한 다음 AddNotification(aUserID, aId, aTitle, aMessage, ...)를 호출합니다. 연결된 사용자는 페이지에서 알림을 받고, 연결되지 않은 사용자는 Send를 한 번 받으므로, 누구에게도 두 번 알리지 않습니다. OnGetChannel 핸들러가 없으면 푸시는 전혀 전송되지 않습니다.
| 온라인 도움말이 찱포넌트의 전체 API 참조 및 사용 가이드입니다. | 열기 | |
| Web Push 가이드각 부분이 어떻게 맞물리는지 설명합니다. 구독, VAPID, 엔진 엔드포인트, 알림 컴포넌트를 다룹니다. | 열기 | |
| 모든 sgcHTML 컴포넌트80개 이상의 컴포넌트 전체 기능 매트릭스를 둘러보십시오. | 열기 | |
| 무료 체험판 다운로드30일 체험판은 60.HTML 데모 프로젝트를 포함하며, 그중 17.FieldService는 Web Push를 사용합니다. | 열기 | |
| 가격전체 소스 코드가 포함된 Single, Team 및 Site 라이선스. | 열기 |