web-push
Web Push library for Node.js
README
web-push
Why
Install
Usage
- ``` js
- const webpush = require('web-push');
- // VAPID keys should be generated only once.
- const vapidKeys = webpush.generateVAPIDKeys();
- webpush.setGCMAPIKey('<Your GCM API Key Here>');
- webpush.setVapidDetails(
- 'mailto:example@yourdomain.org',
- vapidKeys.publicKey,
- vapidKeys.privateKey
- );
- // This is the same output of calling JSON.stringify on a PushSubscription
- const pushSubscription = {
- endpoint: '.....',
- keys: {
- auth: '.....',
- p256dh: '.....'
- }
- };
- webpush.sendNotification(pushSubscription, 'Your Push Payload Text');
- ```
Using VAPID Key for applicationServerKey
- ``` js
- registration.pushManager.subscribe({
- userVisibleOnly: true,
- applicationServerKey: '<Your Public Key from generateVAPIDKeys()>'
- });
- ```
Command Line
- ``` sh
- > web-push generate-vapid-keys --json
- > {"publicKey":"BGtkbcjrO12YMoDuq2sCQeHlu47uPx3SHTgFKZFYiBW8Qr0D9vgyZSZPdw6_4ZFEI9Snk1VEAj2qTYI1I1YxBXE","privateKey":"I0_d0vnesxbBSUmlDdOKibGo6vEXRO-Vu88QlSlm5j0"}
- ```
- ``` js
- {
- "endpoint": "https://fcm.googleapis.com/fcm/send/d61c5u920dw:APA91bEmnw8utjDYCqSRplFMVCzQMg9e5XxpYajvh37mv2QIlISdasBFLbFca9ZZ4Uqcya0ck-SP84YJUEnWsVr3mwYfaDB7vGtsDQuEpfDdcIqOX_wrCRkBW2NDWRZ9qUz9hSgtI3sY",
- "expirationTime": null,
- "keys": {
- "p256dh": "BL7ELU24fJTAlH5Kyl8N6BDCac8u8li_U5PIwG963MOvdYs9s7LSzj8x_7v7RFdLZ9Eap50PiiyF5K0TDAis7t0",
- "auth": "juarI8x__VnHvsOgfeAPHg"
- }
- }
- ```
- ``` sh
- web-push send-notification \
- --endpoint=https://fcm.googleapis.com/fcm/send/d61c5u920dw:APA91bEmnw8utjDYCqSRplFMVCzQMg9e5XxpYajvh37mv2QIlISdasBFLbFca9ZZ4Uqcya0ck-SP84YJUEnWsVr3mwYfaDB7vGtsDQuEpfDdcIqOX_wrCRkBW2NDWRZ9qUz9hSgtI3sY \
- --key=BL7ELU24fJTAlH5Kyl8N6BDCac8u8li_U5PIwG963MOvdYs9s7LSzj8x_7v7RFdLZ9Eap50PiiyF5K0TDAis7t0 \
- --auth=juarI8x__VnHvsOgfeAPHg \
- --vapid-subject=mailto:example@qq.com \
- --vapid-pubkey=BGtkbcjrO12YMoDuq2sCQeHlu47uPx3SHTgFKZFYiBW8Qr0D9vgyZSZPdw6_4ZFEI9Snk1VEAj2qTYI1I1YxBXE \
- --vapid-pvtkey=I0_d0vnesxbBSUmlDdOKibGo6vEXRO-Vu88QlSlm5j0 \
- --payload=Hello
- ```
API Reference
sendNotification(pushSubscription, payload, options)
- ``` js
- const pushSubscription = {
- endpoint: '< Push Subscription URL >',
- keys: {
- p256dh: '< User Public Encryption Key >',
- auth: '< User Auth Secret >'
- }
- };
- const payload = '< Push Payload String >';
- const options = {
- gcmAPIKey: '< GCM API Key >',
- vapidDetails: {
- subject: '< \'mailto\' Address or URL >',
- publicKey: '< URL Safe Base64 Encoded Public Key >',
- privateKey: '< URL Safe Base64 Encoded Private Key >'
- },
- timeout: <Number>
- TTL: <Number>,
- headers: {
- '< header name >': '< header value >'
- },
- contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
- proxy: '< proxy server options >',
- agent: '< https.Agent instance >'
- }
- webpush.sendNotification(
- pushSubscription,
- payload,
- options
- );
- ```
Note: sendNotification() you don't need to define a payload, and this
Input
Note: In order to encrypt the payload, the pushSubscription must
Returns
generateVAPIDKeys()
- ``` js
- const vapidKeys = webpush.generateVAPIDKeys();
- // Prints 2 URL Safe Base64 Encoded Strings
- console.log(vapidKeys.publicKey, vapidKeys.privateKey);
- ```
Input
Returns
Note: You should create these keys once, store them and use them for all
future messages you send.
setGCMAPIKey(apiKey)
- ``` js
- webpush.setGCMAPIKey('Your GCM API Key');
- ```
Input
Returns
encrypt(userPublicKey, userAuth, payload, contentEncoding)
- ``` js
- const pushSubscription = {
- endpoint: 'https://....',
- keys: {
- p256dh: '.....',
- auth: '.....'
- }
- };
- webPush.encrypt(
- pushSubscription.keys.p256dh,
- pushSubscription.keys.auth,
- 'My Payload',
- 'aes128gcm'
- )
- .then(encryptionDetails => {
- });
- ```
(sendNotification will automatically encrypt the payload for you, so if
you use sendNotification you don't need to worry about it).
Input
Returns
getVapidHeaders(audience, subject, publicKey, privateKey, contentEncoding, expiration)
- ``` js
- const parsedUrl = url.parse(subscription.endpoint);
- const audience = parsedUrl.protocol + '//' +
- parsedUrl.hostname;
- const vapidHeaders = vapidHelper.getVapidHeaders(
- audience,
- 'mailto: example@web-push-node.org',
- vapidDetails.publicKey,
- vapidDetails.privateKey,
- 'aes128gcm'
- );
- ```
Input
Returns
generateRequestDetails(pushSubscription, payload, options)
- ``` js
- const pushSubscription = {
- endpoint: '< Push Subscription URL >';
- keys: {
- p256dh: '< User Public Encryption Key >',
- auth: '< User Auth Secret >'
- }
- };
- const payload = '< Push Payload String >';
- const options = {
- gcmAPIKey: '< GCM API Key >',
- vapidDetails: {
- subject: '< \'mailto\' Address or URL >',
- publicKey: '< URL Safe Base64 Encoded Public Key >',
- privateKey: '< URL Safe Base64 Encoded Private Key >',
- }
- TTL: <Number>,
- headers: {
- '< header name >': '< header value >'
- },
- contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
- proxy: '< proxy server options >'
- }
- try {
- const details = webpush.generateRequestDetails(
- pushSubscription,
- payload,
- options
- );
- } catch (err) {
- console.error(err);
- }
- ```
Note: When calling generateRequestDetails() the payload argument
exclude any unnecessary headers.
Headers related to the GCM API Key and / or VAPID keys will be included
if supplied and required.
Input
Note: In order to encrypt the payload, the pushSubscription must
Returns
Browser Support
Browser | Push without Payload | Push with Payload | VAPID | Notes |
---|---|---|---|---|
Chrome | ✓ v42+ | ✓ v50+ | ✓ v52+ | In v51 and less, the `gcm_sender_id` is needed to get a push subscription. |
Edge | ✓ v17+ (April 2018) | ✓ v17+ (April 2018) | ✓ v17+ (April 2018) | |
Firefox | ✓ v44+ | ✓ v44+ | ✓ v46+ | |
Opera | ✓ v39+ * | ✓ v39+ * | ✗ | * Opera supports push on Android but not on desktop. The gcm_sender_id is needed to get a push subscription. |
Safari | ✗ | ✗ | ✗ | |
Samsung Internet Browser | ✓ v4.0.10-53+ | ✓ v5.0.30-40+ | ✗ | The `gcm_sender_id` is needed to get a push subscription. |
Help
Running tests
Prerequisites:
Java JDK or JRE (http://www.oracle.com/technetwork/java/javase/downloads/index.html)