WebPushClient

push. WebPushClient

Send a Web push notification using the web-push npm module, referer to it for details how to generate VAPID credentials to configure this module with 3 required parameters:

Config must have this entry for default included notfications worker to work:

api.app.get("/js/webpush.js", (context, next) {
    context.setHeader("Service-Worker-Allowed", "/");
    next();
});

The device token must be generated in the browser after successful subscription:

Constructor

new WebPushClient(options)

Source:
Parameters:
Name Type Description
options object | Array.<object>
Properties
Name Type Description
key string

VAPID private key

pubkey string

VAPID public key

email string

an admin email for the VAPID subject

Example
const registration = await navigator.serviceWorker.register("/js/webpush.js", { scope: "/" });
const subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: vapidKeyPublic });
app.fetch('/user/update', { data: { pushkey: "webpush://" + window.btoa(JSON.stringify(subscription)) }, type: "POST" });

Methods

send(device, options, callbackopt)

Description:
  • Send web push notification to a browser

Source:
Parameters:
Name Type Attributes Description
device object
options object
Properties
Name Type Attributes Description
msg string <optional>

message text

body string <optional>

A string representing the body text of the notification, which is displayed below the title.

title string <optional>

alert title

badge string <optional>

badge icon url

data object <optional>

Arbitrary data that you want associated with the notification. This can be of any structured-clonable data type.

silent boolean <optional>

A boolean value specifying whether the notification is silent (no sounds or vibrations issued), regardless of the device settings. The default, null, means to respect device defaults. If true, then vibrate must not be present.

tag string <optional>

A string representing an identifying tag for the notification. The default is the empty string.

icon string <optional>

A string containing the URL of an icon to be displayed in the notification.

image string <optional>

A string containing the URL of an image to be displayed in the notification.

dir string <optional>

The direction in which to display the notification. It defaults to auto, which just adopts the browser's language setting behavior, but you can override that behavior by setting values of ltr and rtl (although most browsers seem to ignore these settings.)

lang string <optional>

The notification's language, as specified using a string representing a BCP 47 language tag.

navigate string <optional>

A string containing a URL to navigate to when the user activates the notification. When set, the user agent navigates to this URL instead of firing the notificationclick event. The value is parsed relative to the base URL of the service worker. See Notification.navigate for more information.

renotify boolean <optional>

A boolean value specifying whether the user should be notified after a new notification replaces an old one. The default is false, which means they won't be notified. If true, then tag also must be set.

requireInteraction boolean <optional>

if true, indicates that a notification should remain active until the user clicks or dismisses it, rather than closing automatically.

vibrate Array.<int> <optional>

A vibration pattern for the device's vibration hardware to emit with the notification. If specified, silent must not be true.

callback function() <optional>