The generateRequestDetails() method prepares all the necessary components (endpoint, method, headers, and body) to perform a network request to a push service.
Input
1. pushSubscription
An object containing the subscription details. It should match the format of a PushSubscription object serialized via JSON.stringify() in a browser.
2. payload (Optional)
A string or Node.js Buffer. If provided, it will be encrypted. To encrypt a payload, the pushSubscription must include a keys object with p256dh and auth values. Passing null will return no body and exclude unnecessary headers.
3. options (Optional)
An object containing:
gcmAPIKey: A GCM API key for this specific request.vapidDetails: An object with subject, publicKey, and privateKey (following the VAPID Spec).TTL: Time-to-live in seconds.headers: An object of extra headers to add.contentEncoding: Encoding type (e.g., 'aesgcm' or 'aes128gcm').urgency: Delivery priority (very-low, low, normal, or high).topic: A unique identifier (max 32 characters) for notification coalescing.proxy: Configuration for an HttpsProxyAgent (string URI or options object).
Returns
An object containing:
endpoint: The URL to send the request to.method: Always 'POST'.headers: The headers to include in the request.body: The request body as a Node.js Buffer.
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 >',
urgency:'< Default is normal "Defult" >',
topic:'< Use a maximum of 32 characters from the URL or filename-safe Base64 characters sets. >',
proxy: '< proxy server options >'
}
try {
const details = webpush.generateRequestDetails(
pushSubscription,
payload,
options
);
} catch (err) {
console.error(err);
}