forked from microsoft/workshop-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
58 lines (45 loc) · 1.46 KB
/
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
58
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js'
);
import { clientsClaim } from 'workbox-core';
self.skipWaiting();
clientsClaim();
workbox.precaching.cleanupOutdatedCaches();
console.log("hello world");
self.addEventListener('push', function(event) {
const data = JSON.parse(event.data.text());
event.waitUntil(
registration.showNotification(data.title, {
body: data.message,
icon: 'assets/media/toast.jpg'
})
);
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true })
.then(function(clientList) {
if (clientList.length > 0) {
let client = clientList[0];
for (let i = 0; i < clientList.length; i++) {
if (clientList[i].focused) {
client = clientList[i];
}
}
return client.focus();
}
return clients.openWindow('/');
})
);
});
self.addEventListener('pushsubscriptionchange', function(event) {
event.waitUntil(
Promise.all([
Promise.resolve(event.oldSubscription ? deleteSubscription(event.oldSubscription) : true),
Promise.resolve(event.newSubscription ? event.newSubscription : subscribePush(registration))
.then(function(sub) { return saveSubscription(sub); })
])
);
});
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST || []);