TWebPushNotifications
Below is a list of the most important properties methods and events for the TWebPushNotifications
. Push notifications are tested and supported in: Chrome
, Firefox
, Firefox Developer Edition
, Edge
, Opera
, and on Android
: Chrome
, Firefox
, Opera
, Samsung Browser
.
Registration for push notifications
The RegisterServiceWorker
procedure first registers the service worker, then automatically retrieves a subscription that is tied to that service worker. If the AutoRegisterSubscription
property is set to True
, then it automatically registered the subscriptios on the server via the given RegisterSubscriptionURL
. The UserID
is used as an identificiation, which means it should be unique to the user. At the same time a single UserID
can be registered from different devices.
procedure TForm1.WebButton1Click(Sender: TObject);
begin
WebPushNotifications1.RegistrationUserID := 'UserID';
WebPushNotifications1.RegisterServiceWorker;
end;
Multiple users on the same device
It's possible that there are multiple users who share the same device. They might be interested in different topics or the notifications are personalized and we want to avoid sending a notification to a user who is not entiteled to see it (for example: email services). This can be resolved by introducing a login-logout mechanism. We can send the notifications as long as the user is logged in (= "Active")
. For this purpose the data store has a UserActive
boolean field which identicates if the user is active or not. By default this value is always set to True. If you'd like to modify this value, you can do so by using the Logout or Login methods.
procedure TForm1.WebButton1Click(Sender: TObject);
begin
//Request the server to set UserActive to False
//for the given RegistrationUserID:
WebPushNotifications1.RegistrationUserID := 'UserID';
WebPushNotifications1.Logout;
end;
procedure TForm1.WebButton2Click(Sender: TObject);
begin
//Request the server to set UserActive to True
//for the given RegistrationUserID:
WebPushNotifications1.RegistrationUserID := 'UserID';
WebPushNotifications1.Login;
end;
Properties for TWebPushNotifications
Property | Description |
---|---|
AutoGetSubscription: Boolean | Get the subscription automatically when the VAPID key is received. |
AutoRegisterSubscription: Boolean | Automatically register subscription. |
LoginURL: string | URL for setting the user's active state to True . |
LogoutURL: string | URL for setting the user's active state to False . |
Registration: TJSServiceWorkerRegistration | Provides access to the service worker registration object. |
RegistrationUserID: string | A unique ID for the user (such as email). |
RegistrationUserData: string | Used for setting topics. Use ',' as a separator between the topics. |
RegisterSubscriptionURL: string | URL for registering the subscription. |
ServiceWorkerURL: string | URL for the service worker. |
Subscription: TJSPushSubscription | Provides access to the PushSubscription object. |
UnregisterSubscriptionURL: string | URL for unsubscribing a subscription. |
VapidPublicKey: string | VAPID public key. Can be fetched from the server using VapidPublicKeyURL . |
VapidPublickeyURL: string | URL to fetch the VAPID public key if it's not set yet. |
Methods for TWebPushNotifications
Property | Description |
---|---|
CreateNewSubscription | Method to create a new subscription. |
GetVapidPublicKey | Method to get the VAPID key from the server. |
Login | Method to set the user's active state to True in the data store. |
Logout | Method to set the user's active state to False in the data store. |
RegisterServiceWorker | Method to register the service worker with the browser's push service. It creates a subscription if needed. |
RegisterSubscription | Method to register a subscription on the server. |
Unsubscribe(aAll: Boolean = True) | Method to unregister a subsription on the server. By default all the subscriptions will be unsubscribed that are connected to the UserID. |
Events for TWebPushNotifications
Property | Description |
---|---|
OnGetRegistration | Event triggered when service worker registration is available. |
OnGetSubscription | Event triggered when a subscription is available.s |
OnGetVapidPublicKey | Event triggered when the VAPID public key is fetched from the server. |
OnSubscriptionRegistered | Event triggered when a subscription is sucessfully registered. |
OnUnsubscribed | Event triggered when the user has unsubscribed. |