WebPush notifications only work over HTTPS. Most browsers allow push to work in localhost also for development purposes.
Pre-Requisites
- Integration of JavaScript SDK
Integration Steps
1
Configuration
While creating SuprSend instance you have to pass vapidKey (get it in SuprSend Dashboard —> Vendors —> WebPush).If you want to have custom serviceworker file name instead of
serviceworker.js
(mentioned in step2), you can pass name of it in swFileName
.2
Add ServiceWorker file
Service worker file is the background worker script which handles push notifications.Create
serviceworker.js
file such that it should be publicly accessible from https://<your_domain>/serviceworker.js
. Add below lines of code in that file and replace publicApiKey with key you find in API Keys page in SuprSend Dashboard.3
Register Push
Call Returns:
registerPush
in your code, which will perform following tasks:- Ask for notification permission.
- Register push service and generate webpush token.
- Send webpush token to SuprSend.
Promise<ApiResponse>
NOTE: This method should be called on user action like button click for better UX, don’t call this on page load.