-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
33 lines (29 loc) · 947 Bytes
/
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
/* eslint-disable no-restricted-globals */
const processPushMessage = async (pushEvt) => {
const message = await pushEvt.data.json();
const { title, body, tag } = message;
const options = {
body,
tag,
requireInteraction: true,
icon: './img/favicon.png',
};
return self.registration.showNotification(title, options);
};
const openWindow = async () => {
const openClients = await clients.matchAll({ type: 'window' });
const reusableClient = openClients.find(client => client.url === './new-story.html' && 'focus' in client);
if (reusableClient) {
return reusableClient.focus();
}
return clients.openWindow('./new-story.html');
};
self.addEventListener('push', (evt) => {
const evtClone = evt;
evt.waitUntil(processPushMessage(evtClone));
});
self.addEventListener('notificationclick', (evt) => {
evt.notification.close();
evt.waitUntil(openWindow());
});
/* eslint-enable no-restricted-globals */