Skip to content

Commit e716efb

Browse files
authored
No conversion needed before calling PushManager.subscribe() (#663)
Fixes #662
1 parent 61ea3bf commit e716efb

File tree

1 file changed

+2
-21
lines changed

1 file changed

+2
-21
lines changed

README.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,13 @@ webpush.sendNotification(pushSubscription, 'Your Push Payload Text');
6060

6161
## Using VAPID Key for applicationServerKey
6262

63-
When using your VAPID key in your web app, you'll need to convert the
64-
URL safe base64 string to a Uint8Array to pass into the subscribe call,
63+
When subscribing to push messages, you'll need to pass your VAPID key,
6564
which you can do like so:
6665

6766
```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, '/');
73-
74-
const rawData = window.atob(base64);
75-
const outputArray = new Uint8Array(rawData.length);
76-
77-
for (let i = 0; i < rawData.length; ++i) {
78-
outputArray[i] = rawData.charCodeAt(i);
79-
}
80-
return outputArray;
81-
}
82-
83-
const vapidPublicKey = '<Your Public Key from generateVAPIDKeys()>';
84-
const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);
85-
8667
registration.pushManager.subscribe({
8768
userVisibleOnly: true,
88-
applicationServerKey: convertedVapidKey
69+
applicationServerKey: '<Your Public Key from generateVAPIDKeys()>'
8970
});
9071
```
9172

0 commit comments

Comments
 (0)