-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Gaunt
committed
Oct 14, 2016
1 parent
644a073
commit 1956b52
Showing
3 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,6 +276,131 @@ encryption. | |
- *salt*: A string representing the salt used to encrypt the payload. | ||
- *cipherText*: The encrypted payload as a Buffer. | ||
|
||
<hr /> | ||
|
||
## getVapidHeaders(audience, subject, publicKey, privateKey, expiration) | ||
|
||
```javascript | ||
const parsedUrl = url.parse(subscription.endpoint); | ||
const audience = parsedUrl.protocol + '//' + | ||
parsedUrl.hostname; | ||
|
||
const vapidHeaders = vapidHelper.getVapidHeaders( | ||
audience, | ||
'mailto: [email protected]', | ||
vapidDetails.publicKey, | ||
vapidDetails.privateKey | ||
); | ||
``` | ||
|
||
Encrypts the payload according to the [Message Encryption for Web | ||
Push](https://webpush-wg.github.io/webpush-encryption/) standard. | ||
|
||
> (*sendNotification* will automatically encrypt the payload for you, so if | ||
> you use *sendNotification* you don't need to worry about it). | ||
### Input | ||
|
||
The `getVapidHeaders()` method expects the following input: | ||
|
||
- *audience*: the origin of the **push service**. | ||
- *subject*: the mailto or URL for your application. | ||
- *publicKey*: the VAPID public key. | ||
- *privateKey*: the VAPID private key. | ||
|
||
### Returns | ||
|
||
This method returns an object with the following fields: | ||
|
||
- *localPublicKey*: The public key matched the private key used during | ||
encryption. | ||
- *salt*: A string representing the salt used to encrypt the payload. | ||
- *cipherText*: The encrypted payload as a Buffer. | ||
|
||
<hr /> | ||
|
||
## generateRequestDetails(pushSubscription, payload, options) | ||
|
||
```javascript | ||
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> | ||
} | ||
|
||
try { | ||
const details = webpush.generateRequestDetails( | ||
pushSubscription, | ||
payload, | ||
options | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
``` | ||
|
||
> **Note:** `generateRequestDetails()` you don't need to define a payload, | ||
and this method will return details without a GCM API Key and / or VAPID keys. | ||
|
||
### Input | ||
|
||
**Push Subscription** | ||
|
||
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. | ||
|
||
**Payload** | ||
|
||
The payload is optional, but if set, will be encrypted and a [*Buffer*](https://nodejs.org/api/buffer.html) | ||
will be returned via the `payload` parameter. | ||
|
||
This argument must be either a *string* or a node | ||
[*Buffer*](https://nodejs.org/api/buffer.html). | ||
|
||
> **Note:** In order to encrypt the *payload*, the *pushSubscription* **must** | ||
have a *keys* object with *p256dh* and *auth* values. | ||
|
||
**Options** | ||
|
||
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. | ||
|
||
- **gcmAPIKey** can be a GCM API key to be used for this request and this | ||
request only. This overrides any API key set via `setGCMAPIKey()`. | ||
- **vapidDetails** should be an object with *subject*, *publicKey* and | ||
*privateKey* values defined. These values should follow the [VAPID Spec](https://tools.ietf.org/html/draft-thomson-webpush-vapid). | ||
- **TTL** is a value in seconds that describes how long a push message is | ||
retained by the push service (by default, four weeks); | ||
|
||
### Returns | ||
|
||
An object contains all the details needed to make a network request, the | ||
object will contain: | ||
|
||
- *endpoint*, the URL to send the request to; | ||
- *method*, this will be 'POST'; | ||
- *headers*, the headers to add to the request; | ||
- *body*, the body of the request (As a Node Buffer). | ||
|
||
<hr /> | ||
|
||
# Browser Support | ||
|
||
<table> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters