File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,35 @@ a GCM API key and VAPID keys.
56
56
57
57
webpush.sendNotification(pushSubscription, 'Your Push Payload Text');
58
58
59
+ ## Using VAPID Key for applicationServerKey
60
+
61
+ When using your VAPID key in your web app, you'll need to convert the
62
+ URL safe base64 string in a Uint8Array to pass into the subscribe call,
63
+ which you can do like so:
64
+
65
+ function urlBase64ToUint8Array(base64String) {
66
+ const padding = '='.repeat((4 - base64String.length % 4) % 4);
67
+ const base64 = (base64String + padding)
68
+ .replace(/\-/g, '+')
69
+ .replace(/_/g, '/');
70
+
71
+ const rawData = window.atob(base64);
72
+ const outputArray = new Uint8Array(rawData.length);
73
+
74
+ for (let i = 0; i < rawData.length; ++i) {
75
+ outputArray[i] = rawData.charCodeAt(i);
76
+ }
77
+ return outputArray;
78
+ }
79
+
80
+ const vapidPublicKey = '<Your Public Key from generateVAPIDKeys()>';
81
+ const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);
82
+
83
+ registration.pushManager.subscribe({
84
+ userVisibleOnly: true,
85
+ applicationServerKey: convertedVapidKey
86
+ });
87
+
59
88
# API Referance
60
89
61
90
## sendNotification(pushSubscription, payload, options)
You can’t perform that action at this time.
0 commit comments