-
Notifications
You must be signed in to change notification settings - Fork 2
/
pwa.js
46 lines (41 loc) · 1.08 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
if ('serviceWorker' in navigator)
{
window.addEventListener('load', () =>
{
navigator.serviceWorker.register('service-worker.js').then((registration) =>
{
console.log('Service worker registered:', registration);
},
(error) =>
{
console.log('Service worker registration failed:', error);
});
});
}
else
{
console.log('Service workers are not supported.');
}
// Install prompt
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) =>
{
// Stash the event so it can be triggered later.
deferredPrompt = e;
deferredPrompt.prompt()
.then(res => console.log(res))
.catch(error => console.log(error.message));
// Wait for the user to respond to the prompt
deferredPrompt.userChoice.then((choiceResult) =>
{
if (choiceResult.outcome === 'accepted')
{
console.log('User accepted the A2HS prompt');
}
else
{
console.log('User dismissed the A2HS prompt');
}
deferredPrompt = null;
});
});