TsgcWSServer_API_WebPush is a component that provides functionality for handling WebPush subscriptions. WebPush is a protocol for delivering real-time notifications to web applications that run in the browser. This component can be used to manage subscriptions and send notifications to subscribed clients. Find below the properties, events, and methods provided by TsgcWSServer_API_WebPush class, along with code examples that demonstrate how to use them.
Find below the most important methods.
SendNotification
Use this method to send a notification given a subscription object. The subscription object is just a class with the following properties
The message can be a string or an object of TsgcWebPushMessage
procedure SendNotification(const aSubscription: TsgcHTTP_API_WebPush_PushSubscription);
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;
BroadcastNotification
Use this method to send a Notification to all the clients registered using the Subscriptions property (every time a new client is subscribed, it's added to an internal list. And when the client unsubscribed it's deleted). You can Add or Remove subscription manually using the method Subscriptions.AddSubscription and Subscription.RemoveSubscription.
procedure BroadcastNotification;
var
oMessage: TsgcWebPushMessage;
begin
oMessage := TsgcWebPushMessage.Create;
Try
oMessage.Title := 'eSeGeCe Notification';
oMessage.Body := 'New version released!!!';
oMessage.Icon := 'https://www.esegece.com/images/esegece_logo_small.png';
oMessage.Url := 'https://www.esegece.com';
sgcWSAPIServer_WebPush1.BroadcastNotification(oMessage);
Finally
oMessage.Free;
End;
end;
OnWebPushSubscription
This event is fired when a client subscribes to WebPush notifications. The event handler can be used to store the subscription details on the server-side.
OnWebPushUnsubscription
This event is fired when a client unsubscribes from WebPush notifications. The event handler can be used to remove the subscription details from the server-side.
OnWebPushSendNotificationException
This event is fired when an exception occurs while sending a WebPush notification using the BroadcastNotification method. The event handler can be used to handle the exception and remove the subscription details if required.