add notification

This commit is contained in:
wryk 2021-10-15 18:48:38 +02:00
parent d7032c8aac
commit 7126c5df91
4 changed files with 24 additions and 18 deletions

View file

@ -1,7 +1,7 @@
import store from '~/src/store'
export default () => {
if ('serviceWorker' in navigator && process.env.NODE_ENV === 'production') {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register(new URL('~/src/service-worker.js', import.meta.url), { type: 'module' })
.then(registration => {

View file

@ -16,3 +16,17 @@ async function activate () {
}
addEventListener('activate', e => e.waitUntil(activate()))
addEventListener('notificationclick', event => {
event.notification.close()
event.waitUntil(
clients
.matchAll({ type: 'window' })
.then(clientList => {
for (const client of clientList) {
return client.focus()
}
})
)
})

View file

@ -96,16 +96,12 @@ export default {
this.downloadReady = true
if (document.hidden && ('Notification' in window) && Notification.permission === 'granted') {
const notification = new Notification('You can now download your souvenir', {
body: 'Thank you for your patience.',
icon: appLogo
})
notification.addEventListener('click', () => {
parent.focus()
}, {
once: true
})
navigator.serviceWorker.ready.then((registration) => {
registration.showNotification('You can now download your souvenir', {
body: 'Thank you for your patience.',
icon: appLogo
})
}).catch(console.error)
}
}
})

View file

@ -85,12 +85,8 @@ export default {
}
function showNotificationButton () {
return false
// TODO: fuuuuuuuuuuuuu android, I don't want to spawn notifications from a fucking service worker :<
// return ('Notification' in window) &&
// Notification.permission !== 'granted' &&
// Notification.permission !== 'denied'
return ('Notification' in window) &&
Notification.permission !== 'granted' &&
Notification.permission !== 'denied'
}
</script>