diff options
| author | Egor Kislitsyn <egor@kislitsyn.com> | 2018-12-13 00:03:50 +0700 |
|---|---|---|
| committer | Egor Kislitsyn <egor@kislitsyn.com> | 2018-12-13 00:03:50 +0700 |
| commit | 02c0e15781fa0a499c736e710755e799bfaec77d (patch) | |
| tree | 0defdd796fb4e42e0e228d912622a6bbcc67db89 /src/sw.js | |
| parent | ee70ec4c7efb49c08f0a76b6b2694c0e9910978c (diff) | |
add checkbox to disable web push
Diffstat (limited to 'src/sw.js')
| -rw-r--r-- | src/sw.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/sw.js b/src/sw.js new file mode 100644 index 00000000..6cecb3f3 --- /dev/null +++ b/src/sw.js @@ -0,0 +1,38 @@ +/* eslint-env serviceworker */ + +import localForage from 'localforage' + +function isEnabled () { + return localForage.getItem('vuex-lz') + .then(data => data.config.webPushNotifications) +} + +function getWindowClients () { + return clients.matchAll({ includeUncontrolled: true }) + .then((clientList) => clientList.filter(({ type }) => type === 'window')) +} + +self.addEventListener('push', (event) => { + if (event.data) { + event.waitUntil(isEnabled().then((isEnabled) => { + return isEnabled && getWindowClients().then((list) => { + const data = event.data.json() + + if (list.length === 0) return self.registration.showNotification(data.title, data) + }) + })) + } +}) + +self.addEventListener('notificationclick', (event) => { + event.notification.close() + + event.waitUntil(getWindowClients().then((list) => { + for (var i = 0; i < list.length; i++) { + var client = list[i] + if (client.url === '/' && 'focus' in client) { return client.focus() } + } + + if (clients.openWindow) return clients.openWindow('/') + })) +}) |
