Skip to content

Commit 18d1941

Browse files
author
Matt Gaunt
committed
Adding syntax highlighting
1 parent 5c4810e commit 18d1941

File tree

1 file changed

+97
-85
lines changed

1 file changed

+97
-85
lines changed

README.md

Lines changed: 97 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -33,87 +33,93 @@ Installation is a simple, just install via npm.
3333
The common use case for this library is an application server using
3434
a GCM API key and VAPID keys.
3535

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+
```
5860

5961
## Using VAPID Key for applicationServerKey
6062

6163
When using your VAPID key in your web app, you'll need to convert the
6264
URL safe base64 string in a Uint8Array to pass into the subscribe call,
6365
which you can do like so:
6466

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, '/');
7073

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);
7376

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+
}
7982

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);
8285

83-
registration.pushManager.subscribe({
84-
userVisibleOnly: true,
85-
applicationServerKey: convertedVapidKey
86-
});
86+
registration.pushManager.subscribe({
87+
userVisibleOnly: true,
88+
applicationServerKey: convertedVapidKey
89+
});
90+
```
8791

88-
# API Referance
92+
# API Reference
8993

9094
## sendNotification(pushSubscription, payload, options)
9195

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+
```
117123

118124
### Input
119125

@@ -161,10 +167,12 @@ values on the returned object or error.
161167

162168
## generateVAPIDKeys()
163169

164-
const vapidKeys = webpush.generateVAPIDKeys();
170+
```javascript
171+
const vapidKeys = webpush.generateVAPIDKeys();
165172

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+
```
168176

169177
### Input
170178

@@ -182,7 +190,9 @@ URL Safe Base64 encoded strings.
182190

183191
## setGCMAPIKey(apiKey)
184192

185-
webpush.setGCMAPIKey('Your GCM API Key');
193+
```javascript
194+
webpush.setGCMAPIKey('Your GCM API Key');
195+
```
186196

187197
### Input
188198

@@ -200,21 +210,23 @@ None.
200210

201211
## encrypt(userPublicKey, userAuth, payload)
202212

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+
```
218230

219231
Encrypts the payload according to the [Message Encryption for Web
220232
Push](https://webpush-wg.github.io/webpush-encryption/) standard.

0 commit comments

Comments
 (0)