@@ -33,87 +33,93 @@ Installation is a simple, just install via npm.
33
33
The common use case for this library is an application server using
34
34
a GCM API key and VAPID keys.
35
35
36
- const webpush = require('web-push');
37
-
38
- // VAPID keys should only be generated only once.
39
- const vapidKeys = webpush.generateVAPIDKeys();
40
-
41
- webpush.setGCMAPIKey('<Your GCM API Key Here>');
42
- webpush.setVapidDetails(
43
-
44
- vapidKeys.publicKey,
45
- vapidKeys.privateKey
46
- );
47
-
48
- // This is the same output of calling JSON.stringify on a PushSubscription
49
- const pushSubscription = {
50
- endpoint: '.....',
51
- keys: {
52
- auth: '.....',
53
- p256dh: '.....'
54
- }
55
- };
56
-
57
- webpush.sendNotification(pushSubscription, 'Your Push Payload Text');
36
+ ``` javascript
37
+ const webpush = require (' web-push' );
38
+
39
+ // VAPID keys should only be generated only once.
40
+ const vapidKeys = webpush .generateVAPIDKeys ();
41
+
42
+ webpush .setGCMAPIKey (' <Your GCM API Key Here>' );
43
+ webpush .setVapidDetails (
44
+
45
+ vapidKeys .publicKey ,
46
+ vapidKeys .privateKey
47
+ );
48
+
49
+ // This is the same output of calling JSON.stringify on a PushSubscription
50
+ const pushSubscription = {
51
+ endpoint: ' .....' ,
52
+ keys: {
53
+ auth: ' .....' ,
54
+ p256dh: ' .....'
55
+ }
56
+ };
57
+
58
+ webpush .sendNotification (pushSubscription, ' Your Push Payload Text' );
59
+ ```
58
60
59
61
## Using VAPID Key for applicationServerKey
60
62
61
63
When using your VAPID key in your web app, you'll need to convert the
62
64
URL safe base64 string in a Uint8Array to pass into the subscribe call,
63
65
which you can do like so:
64
66
65
- function urlBase64ToUint8Array(base64String) {
66
- const padding = '='.repeat((4 - base64String.length % 4) % 4);
67
- const base64 = (base64String + padding)
68
- .replace(/\-/g, '+')
69
- .replace(/_/g, '/');
67
+ ``` javascript
68
+ function urlBase64ToUint8Array (base64String ) {
69
+ const padding = ' =' .repeat ((4 - base64String .length % 4 ) % 4 );
70
+ const base64 = (base64String + padding)
71
+ .replace (/ \- / g , ' +' )
72
+ .replace (/ _/ g , ' /' );
70
73
71
- const rawData = window.atob(base64);
72
- const outputArray = new Uint8Array(rawData.length);
74
+ const rawData = window .atob (base64);
75
+ const outputArray = new Uint8Array (rawData .length );
73
76
74
- for (let i = 0; i < rawData.length; ++i) {
75
- outputArray[i] = rawData.charCodeAt(i);
76
- }
77
- return outputArray;
78
- }
77
+ for (let i = 0 ; i < rawData .length ; ++ i) {
78
+ outputArray[i] = rawData .charCodeAt (i);
79
+ }
80
+ return outputArray;
81
+ }
79
82
80
- const vapidPublicKey = '<Your Public Key from generateVAPIDKeys()>';
81
- const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);
83
+ const vapidPublicKey = ' <Your Public Key from generateVAPIDKeys()>' ;
84
+ const convertedVapidKey = urlBase64ToUint8Array (vapidPublicKey);
82
85
83
- registration.pushManager.subscribe({
84
- userVisibleOnly: true,
85
- applicationServerKey: convertedVapidKey
86
- });
86
+ registration .pushManager .subscribe ({
87
+ userVisibleOnly: true ,
88
+ applicationServerKey: convertedVapidKey
89
+ });
90
+ ```
87
91
88
- # API Referance
92
+ # API Reference
89
93
90
94
## sendNotification(pushSubscription, payload, options)
91
95
92
- const pushSubscription = {
93
- endpoint: '< Push Subscription URL >';
94
- keys: {
95
- p256dh: '< User Public Encryption Key >',
96
- auth: '< User Auth Secret >'
97
- }
98
- };
99
-
100
- const payload = '< Push Payload String >';
101
-
102
- const options = {
103
- gcmAPIKey: '< GCM API Key >',
104
- vapidDetails: {
105
- subject: '< \'mailto\' Address or URL >',
106
- publicKey: '< URL Safe Base64 Encoded Public Key >',
107
- privateKey: '< URL Safe Base64 Encoded Private Key >',
108
- }
109
- TTL: <Number>
110
- }
111
-
112
- webpush.sendNotification(
113
- pushSubscription,
114
- payload,
115
- options
116
- );
96
+ ``` javascript
97
+ const pushSubscription = {
98
+ endpoint: ' < Push Subscription URL >' ;
99
+ keys: {
100
+ p256dh: ' < User Public Encryption Key >' ,
101
+ auth: ' < User Auth Secret >'
102
+ }
103
+ };
104
+
105
+ const payload = ' < Push Payload String >' ;
106
+
107
+ const options = {
108
+ gcmAPIKey: ' < GCM API Key >' ,
109
+ vapidDetails: {
110
+ subject: ' < \' mailto\' Address or URL >' ,
111
+ publicKey: ' < URL Safe Base64 Encoded Public Key >' ,
112
+ privateKey: ' < URL Safe Base64 Encoded Private Key >' ,
113
+ }
114
+ TTL : < Number >
115
+ }
116
+
117
+ webpush .sendNotification (
118
+ pushSubscription,
119
+ payload,
120
+ options
121
+ );
122
+ ```
117
123
118
124
### Input
119
125
@@ -161,10 +167,12 @@ values on the returned object or error.
161
167
162
168
## generateVAPIDKeys()
163
169
164
- const vapidKeys = webpush.generateVAPIDKeys();
170
+ ``` javascript
171
+ const vapidKeys = webpush .generateVAPIDKeys ();
165
172
166
- // Prints 2 URL Safe Base64 Encoded Strings
167
- console.log(vapidKeys.publicKey, vapidKeys.privateKey);
173
+ // Prints 2 URL Safe Base64 Encoded Strings
174
+ console .log (vapidKeys .publicKey , vapidKeys .privateKey );
175
+ ```
168
176
169
177
### Input
170
178
@@ -182,7 +190,9 @@ URL Safe Base64 encoded strings.
182
190
183
191
## setGCMAPIKey(apiKey)
184
192
185
- webpush.setGCMAPIKey('Your GCM API Key');
193
+ ``` javascript
194
+ webpush .setGCMAPIKey (' Your GCM API Key' );
195
+ ```
186
196
187
197
### Input
188
198
@@ -200,21 +210,23 @@ None.
200
210
201
211
## encrypt(userPublicKey, userAuth, payload)
202
212
203
- const pushSubscription = {
204
- endpoint: 'https://....',
205
- keys: {
206
- p256dh: '.....',
207
- auth: '.....'
208
- }
209
- };
210
- webPush.encrypt(
211
- pushSubscription.keys.p256dh,
212
- pushSubscription.keys.auth,
213
- 'My Payload'
214
- )
215
- .then(encryptionDetails => {
216
-
217
- });
213
+ ``` javascript
214
+ const pushSubscription = {
215
+ endpoint: ' https://....' ,
216
+ keys: {
217
+ p256dh: ' .....' ,
218
+ auth: ' .....'
219
+ }
220
+ };
221
+ webPush .encrypt (
222
+ pushSubscription .keys .p256dh ,
223
+ pushSubscription .keys .auth ,
224
+ ' My Payload'
225
+ )
226
+ .then (encryptionDetails => {
227
+
228
+ });
229
+ ```
218
230
219
231
Encrypts the payload according to the [ Message Encryption for Web
220
232
Push] ( https://webpush-wg.github.io/webpush-encryption/ ) standard.
0 commit comments