Web push requires that push messages triggered from a backend be done via the Web Push Protocol and if you want to send data with your push message, you must also encrypt that data according to the Message Encryption for Web Push spec. This module makes it easy to send messages and will also handle legacy support for browsers relying on GCM for message sending / delivery. Installation is simple, just install via npm. The common use case for this library is an application server using a GCM API key and VAPID keys. You can install web-push globally and use it for sending notifications and / or generating VAPID keys. Note: sendNotification() you don't need to define a payload, and this method will work without a GCM API Key and / or VAPID keys if the push service supports it. The first argument must be an object containing the details for a push subscription. The expected format is the same output as JSON.stringify'ing a PushSubscription in the browser.
The payload is optional, but if set, will be the data sent with a push message. This must be either a string or a node Buffer. Note: In order to encrypt the payload, the pushSubscription must have a keys object with p256dh and auth values. Options is an optional argument that if defined should be an object containing any of the following values defined, although none of them are required. APIKey can be a GCM API key to be used for this request and this request only. This overrides any API key set via setGCMAPIKey(). Details should be an object with subject, publicKey and privateKey values defined. These values should follow the VAPID Spec. On timeout, the request will be destroyed and the promise will be rejected with a meaningful error. It's a common misconception that a socket timeout is the timeout to receive the full response. So if you have a socket timeout of 1 second, and a response comprised of 3 TCP packets, where each response packet takes 0.9 seconds to arrive, for a total response time of 2.7 seconds, then there will be no timeout.
Once a socket 'timeout' triggers the request will be aborted by the library (by default undefined). TTL is a value in seconds that describes how long a push message is retained by the push service (by default, four weeks). Encoding is the type of push encoding to use (e.g. 'aes128gcm', by default, or 'aesgcm'). ’s device power considerations for delivery. Provide one of the following values: very-low, low, normal, or high. To attempt to deliver the notification immediately, specify high. Use a maximum of 32 characters from the URL or filename-safe Base64 characters sets. HttpsProxyAgent's constructor argument that may either be a string URI of the proxy server (eg. HTTPS Agent instance which will be used in the https.request method. If the proxy options defined, agent will be ignored! Note: As of this writing, if a push notification request contains a VAPID subject referencing an https://localhost URI (set either using the options argument or via the global setVapidDetails() method), Safari's push notification endpoint rejects the request with a BadJwtToken error.
A promise that resolves if the notification was sent successfully with details of the request, otherwise it rejects. In both cases, resolving or rejecting, you'll be able to access the following values on the returned object or error. Returns an object with publicKey and privateKey values which are URL Safe Base64 encoded strings. Note: You should create these keys once, store them and use them for all future messages you send. You can use a GCM API Key from the Google Developer Console or the Cloud Messaging tab under a Firebase Project. Globally sets the application's VAPID subject, public key, and private key, to be used in subsequent calls to sendNotification() and generateRequestDetails() that don't specifically override them in their options argument. VAPID server contact information, as either an https: or mailto: URI (as per the VAPID spec). Key: the VAPID public key. Key: the VAPID private key. Encrypts the payload according to the Message Encryption for Web Push standard.
PublicKey: The public key matched the private key used during encryption. A string representing the salt used to encrypt the payload. Text: The encrypted payload as a Buffer. The getVapidHeaders() method will take in the values needed to create an Authorization and Crypto-Key header. URL for your application. Key: the VAPID public key. Key: the VAPID private key. Encoding: the type of content encoding to use (e.g. aesgcm or aes128gcm). PublicKey: The public key matched the private key used during encryption. A string representing the salt used to encrypt the payload. Text: The encrypted payload as a Buffer. Note: When calling generateRequestDetails() the payload argument does not need to be defined, passing in null will return no body and exclude any unnecessary headers. Headers related to the GCM API Key and / or VAPID keys will be included if supplied and required. The first argument must be an object containing the details for a push subscription.
|