@@ -276,6 +276,131 @@ encryption.
276276- * salt* : A string representing the salt used to encrypt the payload.
277277- * cipherText* : The encrypted payload as a Buffer.
278278
279+ <hr />
280+
281+ ## getVapidHeaders(audience, subject, publicKey, privateKey, expiration)
282+
283+ ``` javascript
284+ const parsedUrl = url .parse (subscription .endpoint );
285+ const audience = parsedUrl .protocol + ' //' +
286+ parsedUrl .hostname ;
287+
288+ const vapidHeaders = vapidHelper .getVapidHeaders (
289+ audience,
290+ 291+ vapidDetails .publicKey ,
292+ vapidDetails .privateKey
293+ );
294+ ```
295+
296+ Encrypts the payload according to the [ Message Encryption for Web
297+ Push] ( https://webpush-wg.github.io/webpush-encryption/ ) standard.
298+
299+ > (* sendNotification* will automatically encrypt the payload for you, so if
300+ > you use * sendNotification* you don't need to worry about it).
301+
302+ ### Input
303+
304+ The ` getVapidHeaders() ` method expects the following input:
305+
306+ - * audience* : the origin of the ** push service** .
307+ - * subject* : the mailto or URL for your application.
308+ - * publicKey* : the VAPID public key.
309+ - * privateKey* : the VAPID private key.
310+
311+ ### Returns
312+
313+ This method returns an object with the following fields:
314+
315+ - * localPublicKey* : The public key matched the private key used during
316+ encryption.
317+ - * salt* : A string representing the salt used to encrypt the payload.
318+ - * cipherText* : The encrypted payload as a Buffer.
319+
320+ <hr />
321+
322+ ## generateRequestDetails(pushSubscription, payload, options)
323+
324+ ``` javascript
325+ const pushSubscription = {
326+ endpoint: ' < Push Subscription URL >' ;
327+ keys: {
328+ p256dh: ' < User Public Encryption Key >' ,
329+ auth: ' < User Auth Secret >'
330+ }
331+ };
332+
333+ const payload = ' < Push Payload String >' ;
334+
335+ const options = {
336+ gcmAPIKey: ' < GCM API Key >' ,
337+ vapidDetails: {
338+ subject: ' < \' mailto\' Address or URL >' ,
339+ publicKey: ' < URL Safe Base64 Encoded Public Key >' ,
340+ privateKey: ' < URL Safe Base64 Encoded Private Key >' ,
341+ }
342+ TTL : < Number >
343+ }
344+
345+ try {
346+ const details = webpush .generateRequestDetails (
347+ pushSubscription,
348+ payload,
349+ options
350+ );
351+ } catch (err) {
352+ console .error (err);
353+ }
354+ ```
355+
356+ > ** Note:** ` generateRequestDetails() ` you don't need to define a payload,
357+ and this method will return details without a GCM API Key and / or VAPID keys.
358+
359+ ### Input
360+
361+ ** Push Subscription**
362+
363+ The first argument must be an object containing the details for a push
364+ subscription.
365+
366+ The expected format is the same output as JSON.stringify'ing a PushSubscription
367+ in the browser.
368+
369+ ** Payload**
370+
371+ The payload is optional, but if set, will be encrypted and a [ * Buffer* ] ( https://nodejs.org/api/buffer.html )
372+ will be returned via the ` payload ` parameter.
373+
374+ This argument must be either a * string* or a node
375+ [ * Buffer* ] ( https://nodejs.org/api/buffer.html ) .
376+
377+ > ** Note:** In order to encrypt the * payload* , the * pushSubscription* ** must**
378+ have a * keys* object with * p256dh* and * auth* values.
379+
380+ ** Options**
381+
382+ Options is an optional argument that if defined should be an object containing
383+ any of the following values defined, although none of them are required.
384+
385+ - ** gcmAPIKey** can be a GCM API key to be used for this request and this
386+ request only. This overrides any API key set via ` setGCMAPIKey() ` .
387+ - ** vapidDetails** should be an object with * subject* , * publicKey* and
388+ * privateKey* values defined. These values should follow the [ VAPID Spec] ( https://tools.ietf.org/html/draft-thomson-webpush-vapid ) .
389+ - ** TTL** is a value in seconds that describes how long a push message is
390+ retained by the push service (by default, four weeks);
391+
392+ ### Returns
393+
394+ An object contains all the details needed to make a network request, the
395+ object will contain:
396+
397+ - * endpoint* , the URL to send the request to;
398+ - * method* , this will be 'POST';
399+ - * headers* , the headers to add to the request;
400+ - * body* , the body of the request (As a Node Buffer).
401+
402+ <hr />
403+
279404# Browser Support
280405
281406<table >
0 commit comments