Skip to content

Commit cc328bc

Browse files
HGdawymarco-c
andauthored
Add support for Urgency and Topic optional parameters (#764)
Co-authored-by: Marco Castelluccio <[email protected]>
1 parent 96aa6b1 commit cc328bc

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ const options = {
133133
'< header name >': '< header value >'
134134
},
135135
contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
136+
urgency:'< Default is "normal" >',
137+
topic:'< Use a maximum of 32 characters from the URL or filename-safe Base64 characters sets. >',
138+
136139
proxy: '< proxy server options >',
137140
agent: '< https.Agent instance >'
138141
}
@@ -183,6 +186,8 @@ request only. This overrides any API key set via `setGCMAPIKey()`.
183186
retained by the push service (by default, four weeks).
184187
- **headers** is an object with all the extra headers you want to add to the request.
185188
- **contentEncoding** is the type of push encoding to use (e.g. 'aes128gcm', by default, or 'aesgcm').
189+
- **urgency** is to indicate to the push service whether to send the notification immediately or prioritize the recipient’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.
190+
- **topic** optionally provide an identifier that the push service uses to coalesce notifications. Use a maximum of 32 characters from the URL or filename-safe Base64 characters sets.
186191
- **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options)
187192
that may either be a string URI of the proxy server (eg. http://< hostname >:< port >)
188193
or an "options" object with more specific properties.
@@ -357,6 +362,8 @@ const options = {
357362
'< header name >': '< header value >'
358363
},
359364
contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >',
365+
urgency:'< Default is normal "Defult" >',
366+
topic:'< Use a maximum of 32 characters from the URL or filename-safe Base64 characters sets. >',
360367
proxy: '< proxy server options >'
361368
}
362369

@@ -411,6 +418,8 @@ request only. This overrides any API key set via `setGCMAPIKey()`.
411418
retained by the push service (by default, four weeks).
412419
- **headers** is an object with all the extra headers you want to add to the request.
413420
- **contentEncoding** is the type of push encoding to use (e.g. 'aesgcm', by default, or 'aes128gcm').
421+
- **urgency** is to indicate to the push service whether to send the notification immediately or prioritize the recipient’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.
422+
- **topic** optionally provide an identifier that the push service uses to coalesce notifications. Use a maximum of 32 characters from the URL or filename-safe Base64 characters sets.
414423
- **proxy** is the [HttpsProxyAgent's constructor argument](https://github.com/TooTallNate/node-https-proxy-agent#new-httpsproxyagentobject-options)
415424
that may either be a string URI of the proxy server (eg. http://< hostname >:< port >)
416425
or an "options" object with more specific properties.
@@ -509,15 +518,15 @@ object will contain:
509518
<td>Safari</td>
510519

511520
<!-- Push without payloads support-->
512-
<td></td>
521+
<td>✓ v16+ </td>
513522

514523
<!-- Push with payload support -->
515-
<td></td>
524+
<td>✓ v16+</td>
516525

517526
<!-- VAPID Support -->
518-
<td></td>
527+
<td>✓ v16+</td>
519528

520-
<td></td>
529+
<td>Safari 16 in macOS 13 or later</td>
521530
</tr>
522531

523532
<tr>

src/web-push-constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ WebPushConstants.supportedContentEncodings = {
77
AES_128_GCM: 'aes128gcm'
88
};
99

10+
WebPushConstants.supportedUrgency = {
11+
VERY_LOW: 'very-low',
12+
LOW: 'low',
13+
NORMAL: 'normal',
14+
HIGH: 'high'
15+
};
16+
1017
module.exports = WebPushConstants;

src/web-push-lib.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
109109
let timeToLive = DEFAULT_TTL;
110110
let extraHeaders = {};
111111
let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM;
112+
let urgency = webPushConstants.supportedUrgency.NORMAL;
113+
let topic;
112114
let proxy;
113115
let agent;
114116
let timeout;
@@ -120,6 +122,8 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
120122
'vapidDetails',
121123
'TTL',
122124
'contentEncoding',
125+
'urgency',
126+
'topic',
123127
'proxy',
124128
'agent',
125129
'timeout'
@@ -173,6 +177,27 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
173177
}
174178
}
175179

180+
if (options.urgency) {
181+
if ((options.urgency === webPushConstants.supportedUrgency.VERY_LOW
182+
|| options.urgency === webPushConstants.supportedUrgency.LOW
183+
|| options.urgency === webPushConstants.supportedUrgency.NORMAL
184+
|| options.urgency === webPushConstants.supportedUrgency.HIGH)) {
185+
urgency = options.urgency;
186+
} else {
187+
throw new Error('Unsupported urgency specified.');
188+
}
189+
}
190+
191+
if (options.Topic) {
192+
if (!urlBase64.validate(options.topic)) {
193+
throw new Error('Unsupported characters set use the URL or filename-safe Base64 characters set');
194+
}
195+
if (options.topic.length > 32) {
196+
throw new Error('use maximum of 32 characters from the URL or filename-safe Base64 characters set');
197+
}
198+
topic = options.Topic;
199+
}
200+
176201
if (options.proxy) {
177202
if (typeof options.proxy === 'string'
178203
|| typeof options.proxy.host === 'string') {
@@ -272,6 +297,12 @@ WebPushLib.prototype.generateRequestDetails = function(subscription, payload, op
272297
requestDetails.headers.Authorization = 'key=' + currentGCMAPIKey;
273298
}
274299

300+
requestDetails.headers.Urgency = urgency;
301+
302+
if (topic) {
303+
requestDetails.headers.Topic = topic;
304+
}
305+
275306
requestDetails.body = requestPayload;
276307
requestDetails.endpoint = subscription.endpoint;
277308

test/test-generate-request-details.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ suite('Test Generate Request Details', function() {
258258
TTL: 100,
259259
headers: {
260260
'Topic': 'topic',
261-
'Urgency': 'urgency'
261+
'Urgency': 'normal'
262262
}
263263
};
264264
let details = webPush.generateRequestDetails(

0 commit comments

Comments
 (0)