From 7126c5df91f784c52f8dfbe066ee82e13da03c06 Mon Sep 17 00:00:00 2001 From: wryk Date: Fri, 15 Oct 2021 18:48:38 +0200 Subject: [PATCH] add notification --- src/register-service-worker.js | 2 +- src/service-worker.js | 14 ++++++++++++++ src/views/screens/download.vue | 16 ++++++---------- src/views/screens/encoding.vue | 10 +++------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/register-service-worker.js b/src/register-service-worker.js index 2cad35d..fa55a89 100644 --- a/src/register-service-worker.js +++ b/src/register-service-worker.js @@ -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 => { diff --git a/src/service-worker.js b/src/service-worker.js index 27c4879..f18388e 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -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() + } + }) + ) +}) diff --git a/src/views/screens/download.vue b/src/views/screens/download.vue index a65dfb5..30f0215 100644 --- a/src/views/screens/download.vue +++ b/src/views/screens/download.vue @@ -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) } } }) diff --git a/src/views/screens/encoding.vue b/src/views/screens/encoding.vue index e91d38c..893e52d 100644 --- a/src/views/screens/encoding.vue +++ b/src/views/screens/encoding.vue @@ -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' }