WebPush
TsgcHTMLWebPush: çalışmayan ya da sayfanızda olmayan bir tarayıcı için Web Push, Delphi, C++ Builder ve .NET'te. Yalnızca Enterprise ve All-Access sürümlerinde kullanılabilir.
TsgcHTMLWebPush: çalışmayan ya da sayfanızda olmayan bir tarayıcı için Web Push, Delphi, C++ Builder ve .NET'te. Yalnızca Enterprise ve All-Access sürümlerinde kullanılabilir.
Web Push, tarayıcısı kapalı olan ya da başka bir sayfa gösteren bir kullanıcıya ulaşır. Sayfa abone olur, uygulamanız aboneliği üç olay aracılığıyla saklar ve tek bir Send çağrısı bildirimi teslim eder. Yalnızca Enterprise ve All-Access sürümlerinde kullanılabilir.
TsgcHTMLWebPush (sgcHTML_WebPush birimi)
İşaretleme yok: tarayıcıya gönderilen şifrelenmiş push iletileri
Delphi, C++ Builder, .NET
Bir VAPID anahtar çiftini bir kez üretin ve saklayın. Çifti ve bir Subject atayın, depolama olaylarını işleyin, motorda WebPush ve şablonunda WebPushEnabled özelliğini ayarlayın, ardından Send çağırın.
// 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>
En sık başvurduğunuz üyeler.
Web Push yalnızca Enterprise ve All-Access sürümlerinde kullanılabilir. Birim, SGC_WEBPUSH tanımlı olduğunda derlenir; sgcVer.inc bunu bu iki sürüm için tanımlar ve ayrıca bağımsız olarak satılan sgcHTML'in kendisini de gerektirir. Bildirim gelen kutusu ve tercihler tablosu sgcHTML'in parçasıdır ve buna ihtiyaç duymaz: yalnızca push kanalı gerektirir.
GenerateVAPIDKeys yeni bir çift döndüren bir sınıf yöntemidir: genel yarı, tarayıcının abone olurken kullandığı base64url uygulama sunucusu anahtarı, özel yarı ise PEM biçiminde bir EC anahtarıdır. Bunları VAPIDPublicKey ve VAPIDPrivateKey özelliklerine atayın. Subject VAPID iletişim bilgisidir, bir mailto: ya da https: URI'sidir. Standart Delphi derlemesi kriptografi için OpenSSL 3.x yükler ve bu API'yi kendisi seçer; .NET portu yönetilen kriptografi kullanır ve OpenSSL gerektirmez.
Bileşen kendi başına hiçbir abonelik tutmaz. OnSaveSubscription(aUserID, aEndpoint, aP256dh, aAuth) bir tanesini saklar, OnLoadSubscriptions(aUserID, aList), aList listesini kullanıcının saklanan her aboneliği için bir Add(endpoint, p256dh, auth) çağrısıyla doldurur ve OnDeleteSubscription(aUserID, aEndpoint) bir tanesini kaldırır. Her girdi bir TsgcHTMLWebPushSubscription nesnesidir.
Send(aUserID, aTitle, aBody, aURL, aIcon, aTag, aTTL, aUrgency) kullanıcının saklanan her aboneliği için bir bildirimi şifreler (RFC 8291) ve bir VAPID imzasıyla push hizmetine POST eder (RFC 8292). Kaç aboneliğe teslim ettiğini döndürür, Enabled değeri False ise 0 döndürür. Gönderici olan TsgcHTMLWebPushClient, TsgcHTTP_API_WebPush_Client sınıfını yeniden kullanır.
TTL varsayılan olarak 2419200 saniyedir (28 gün) ve Urgency boştur, bu da hiçbir aciliyet üst bilgisi göndermez. Bunlardan birini tek bir bildirim için geçersiz kılmak üzere aTTL ya da aUrgency değerini Send yöntemine geçirin.
Bir push hizmeti 404 ya da 410 ile yanıt verdiğinde aboneliği silebilmeniz için OnSubscriptionExpired(aUserID, aEndpoint, aStatusCode) tetiklenir. Diğer her hata OnSendError(aUserID, aEndpoint, E) olayını yükseltir ve kalan abonelikler yine de denenir.
Bileşen motora atandıktan sonra, SubscribeEndpoint (/push/subscribe) ya da UnsubscribeEndpoint (/push/unsubscribe) adresine yapılan bir POST sizin yerinize yanıtlanır: işlendiğinde 204, geçersiz bir abonelik için 400, hiçbir kullanıcı çözümlenmediğinde 403, 64 KB üzerinde 413. POST isteklerini kendi yönteminizle yönlendirmek için HandleSubscribe(aJSON, aUserID) ve HandleUnsubscribe yöntemlerini kendiniz çağırın.
Kullanıcı hiçbir zaman tarayıcının gönderdiği veriden okunmaz. Motorda bir TsgcHTMLAuth varsa kullanıcı, oturum çerezinin kullanıcısıdır ve CSRF belirteci gereklidir. Yoksa OnResolveUser(aCookieHeader, aHeaders, aBody, aUserID, aAllowed) kendi kullanıcı kimliğinizle yanıt verir ya da reddeder. İkisi de yoksa istek 403 alır.
Template.WebPushEnabled açıkken motor genel anahtarı ve uç noktaları şablona kopyalar, service worker'ı ServiceWorkerPath adresinden sunar ve sayfa abonelik betiğini taşır. İzin isteminin bir tıklamayla çalışması için sayfaya <button data-sgc-webpush> ekleyin; data-sgc-webpush="unsubscribe" abonelikten çıkarır. Tarayıcılar https gerektirir, geliştirme sırasında ise localhost yeterlidir.
İkisi arasında seçimi TsgcHTMLComponent_Notification yapar. Presence ve WebPush özelliklerini atayın, OnGetChannel olayını ncPush ile yanıtlayın ve AddNotification(aUserID, aId, aTitle, aMessage, ...) çağırın: bağlı bir kullanıcı bildirimi sayfada alır, bağlı olmayan bir kullanıcı bir Send alır, böylece kimseye iki kez bildirilmez. OnGetChannel işleyicisi olmadan hiçbir şey push edilmez.
| Çevrimiçi yardımBu bileşen için tam API referansı ve kullanım kılavuzu. | Aç | |
| Web Push kılavuzuParçaların birbirine nasıl uyduğu: abonelikler, VAPID, motor uç noktaları ve bildirim bileşeni. | Aç | |
| Tüm sgcHTML Bileşenleri80'den fazla bileşenin tam özellik matrisine göz atın. | Aç | |
| Ücretsiz Deneme Sürümünü İndirin30 günlük deneme sürümü, Web Push kullanan 17.FieldService dahil olmak üzere 60.HTML demo projelerini içerir. | Aç | |
| FiyatlandırmaTam kaynak kodlu Single, Team ve Site lisansları. | Aç |