Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 1.66 KB

web-push.md

File metadata and controls

54 lines (37 loc) · 1.66 KB

Web Push

Web Push delivers to browsers through the standard Push API. node-pushnotifications sends through web-push and authenticates with VAPID.

Credentials

Generate a VAPID key pair once and keep it:

npx web-push generate-vapid-keys

Put both keys in your settings:

const push = new PushNotifications({
  web: {
    vapidDetails: {
      subject: 'mailto:you@example.com',
      publicKey: '<VAPID public key>',
      privateKey: '<VAPID private key>',
    },
    TTL: 2419200,
    contentEncoding: 'aes128gcm',
  },
});

In the browser, pass the public key to pushManager.subscribe({ applicationServerKey }).

Settings

settings.web is passed to web-push's sendNotification as its options, so TTL, headers, contentEncoding, urgency and proxy all work.

Sending

The destination is the PushSubscription your service worker got from pushManager.subscribe():

await push.send([{ endpoint: 'https://...', keys: { p256dh: '<key>', auth: '<secret>' } }], {
  title: 'New message',
  body: 'You have a new message from Ana',
});

The whole data value is the payload. Strings are sent as they are, and anything else is JSON-stringified. Your service worker receives it in its push event and decides how to show it.

Proxies

Set proxy in settings.web.

Example

alex-friedl/webpush-example has a complete client and server.