-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation_sw.js
57 lines (49 loc) · 1.19 KB
/
location_sw.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 log(message) {
console.log(message);
}
function sendMessage(message) {
log('sendMessage: ' + message);
clients.getAll().then(clients => {
log(clients.length + ' clients');
clients.forEach(client => {
client.postMessage(message);
});
});
}
log('In service worker');
oninstall = event => {
log(event.type + ' event');
event.waitUntil(skipWaiting());
};
onactivate = event => {
log(event.type + ' event');
event.waitUntil(clients.claim());
};
onfetch = event => {
log('fetch url: ' + event.request.url);
};
onnotificationclose = event => {
log(event.type + ' event');
};
onnotificationclick = event => {
log(event.type + ' event');
event.notification.close();
};
onmessage = event => {
log(event.type + ' event');
log(event.data + ' command');
switch (event.data) {
case 'showNotification':
registration.showNotification('Persistent notification', {
icon: '/sandbox/resources/11.png',
body: 'From service worker'
});
break;
case 'fetch':
fetch('/sandbox/resources/11.png');
break;
default:
log('Unhandled message: ' + event.data);
}
sendMessage('Received message: ' + event.data);
};