2018-12-06 05:34:00 -08:00
|
|
|
/* eslint-env serviceworker */
|
|
|
|
|
2018-12-09 04:25:43 -08:00
|
|
|
function getWindowClients () {
|
|
|
|
return clients.matchAll({ includeUncontrolled: true })
|
|
|
|
.then((clientList) => clientList.filter(({ type }) => type === 'window'))
|
|
|
|
}
|
|
|
|
|
|
|
|
self.addEventListener('push', (event) => {
|
|
|
|
if (event.data) {
|
|
|
|
event.waitUntil(getWindowClients().then((list) => {
|
|
|
|
const data = event.data.json()
|
2018-12-06 05:34:00 -08:00
|
|
|
|
2018-12-09 04:25:43 -08:00
|
|
|
if (list.length === 0) return self.registration.showNotification(data.title, data)
|
|
|
|
}))
|
2018-12-06 05:34:00 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-09 04:25:43 -08:00
|
|
|
self.addEventListener('notificationclick', (event) => {
|
2018-12-06 05:34:00 -08:00
|
|
|
event.notification.close()
|
|
|
|
|
2018-12-09 04:25:43 -08:00
|
|
|
event.waitUntil(getWindowClients().then((list) => {
|
2018-12-06 05:34:00 -08:00
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
var client = list[i]
|
|
|
|
if (client.url === '/' && 'focus' in client) { return client.focus() }
|
|
|
|
}
|
2018-12-09 04:25:43 -08:00
|
|
|
|
|
|
|
if (clients.openWindow) return clients.openWindow('/')
|
2018-12-06 05:34:00 -08:00
|
|
|
}))
|
|
|
|
})
|