-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.js
79 lines (68 loc) · 2.21 KB
/
site.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
function log(text) {
const statusElement = document.getElementById('status');
statusElement.innerText += text;
statusElement.appendChild(document.createElement("br"));
}
function registerServiceWorker() {
return navigator.serviceWorker.register('/service-worker.js')
.then(function (registration) {
log('Service worker successfully registered.');
return registration;
})
.catch(function (err) {
log('Unable to register service worker.' + err);
});
}
function askPermission() {
return new Promise(function (resolve, reject) {
const permissionResult = Notification.requestPermission(function (result) {
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
}).then(function (permissionResult) {
if (permissionResult !== 'granted') {
log('We weren\'t granted permission.');
}
});
}
/**
* urlBase64ToUint8Array
*
* @param {string} base64String a public vavid key
*/
function urlBase64ToUint8Array(base64String) {
var padding = '='.repeat((4 - base64String.length % 4) % 4);
var base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
var rawData = window.atob(base64);
var outputArray = new Uint8Array(rawData.length);
for (var i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
function subscribe(serviceWorker) {
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(
'BC2u_b8s1ROBt4lN88ZSuCyTs5C_ghCZmcknziO2y6xWWzuMj7VQGcVygQ98XB3TXKE0Z2DJOgzqTTD7-lFqGOg'
)
};
return serviceWorker.pushManager.subscribe(subscribeOptions).then(function (pushSubscription) {
log(JSON.stringify(pushSubscription));
}).catch(function (err) {
log('Unable to subscribe: ' + err);
});
}
async function run() {
await askPermission();
var serviceWorker = await registerServiceWorker();
return await subscribe(serviceWorker);
}
if ('serviceWorker' in navigator && 'PushManager' in window) {
run();
}