Web Push delivers to browsers through the standard Push API. node-pushnotifications sends through web-push and authenticates with VAPID.
Generate a VAPID key pair once and keep it:
npx web-push generate-vapid-keysPut 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.web is passed to web-push's sendNotification as its options, so TTL, headers, contentEncoding, urgency and proxy all work.
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.
Set proxy in settings.web.
alex-friedl/webpush-example has a complete client and server.