From 458e5e34d8eaa11ce1406c1afde23581f0fac6f6 Mon Sep 17 00:00:00 2001 From: Vasilii Mikhailovskii Date: Thu, 21 Nov 2024 16:51:51 +0200 Subject: [PATCH] update service worker --- apps/frontend/public/service-worker.js | 32 +++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/apps/frontend/public/service-worker.js b/apps/frontend/public/service-worker.js index 6c530b2..3c09943 100644 --- a/apps/frontend/public/service-worker.js +++ b/apps/frontend/public/service-worker.js @@ -5,14 +5,14 @@ self.addEventListener('push', (event) => { // Отображение уведомления if (data.type === 'notification') { event.waitUntil( - self.registration.showNotification(data.notification.title, data.notification.options).then( - () => { + self.registration + .showNotification(data.notification.title, data.notification.options) + .then(() => { console.log('Notification создан', data.notification.options.tag) - }, - (e) => { - console.error('Notification ошибка создания', e) - }, - ), + }) + .catch((error) => { + console.error('Notification ошибка создания', error) + }), ) } else if (data.type === 'cancelNotification') { event.waitUntil( @@ -33,9 +33,16 @@ self.addEventListener('push', (event) => { } }) +self.addEventListener('notificationclose', function (event) { + const dismissedNotification = event.notification + + console.log('dismissedNotification', dismissedNotification) +}) + self.addEventListener('notificationclick', (event) => { console.log('клик на уведомление', event.notification) - event.notification.close() + + const urlToOpen = new URL(event.notification.data.url, self.location.origin).href // Открытие URL из данных уведомления event.waitUntil( @@ -44,7 +51,7 @@ self.addEventListener('notificationclick', (event) => { .then((clientList) => { for (const client of clientList) { console.log('client.url', client.url) - if (client.url === event.notification.data.url && 'focus' in client) { + if (client.url === urlToOpen && 'focus' in client) { console.log('фокус') return Promise.all([ @@ -58,8 +65,8 @@ self.addEventListener('notificationclick', (event) => { } } if (clients.openWindow) { - console.log('новое окно') - return clients.openWindow(event.notification.data.url).then((client) => { + console.log('новое окно', urlToOpen) + return clients.openWindow(urlToOpen).then((client) => { if (client) { return client.focus() } @@ -67,6 +74,9 @@ self.addEventListener('notificationclick', (event) => { }) } }) + .then(() => { + return event.notification.close() + }) .catch((error) => { console.error(error) }),