-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwa.js
57 lines (52 loc) · 2.39 KB
/
pwa.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
function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
if ('serviceWorker' in navigator && 'PushManager' in window) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
registration.pushManager.getSubscription().then(function(subscription) {
if (subscription === null) {
console.log('User is NOT subscribed. Subscribing...');
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array('BN16o4Vep3uUcxOJmhlHO56Mi_m6Kz-Bepj0f05x86DqsrdJgHpeyZRqgDpiod4zMlPWMyxdKRBW2I0hUALZeCw')
}).then(function(subscription) {
console.log('User is subscribed');
console.log(subscription.endpoint);
// TODO notify the app server about this new subscription endpoint
// let data = {
// appid: "com.moodle.moodlemobile",
// name: "",
// model: "",
// platform: "",
// version: "",
// pushid: subscription.endpoint,
// uuid: ""
// };
// 'core_user_add_user_device', data
}).catch(function(err) {
console.log('Failed to subscribe the user: ', err);
});
} else {
console.log('User IS subscribed.');
}
});
}, function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
window.addEventListener('beforeinstallprompt', function(e) {
e.preventDefault();
e.prompt();
});