update service worker
Main daploy / deploy (push) Successful in 53s

This commit is contained in:
2024-11-21 16:51:51 +02:00
parent 75be87c144
commit 458e5e34d8
+21 -11
View File
@@ -5,14 +5,14 @@ self.addEventListener('push', (event) => {
// Отображение уведомления // Отображение уведомления
if (data.type === 'notification') { if (data.type === 'notification') {
event.waitUntil( 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) console.log('Notification создан', data.notification.options.tag)
}, })
(e) => { .catch((error) => {
console.error('Notification ошибка создания', e) console.error('Notification ошибка создания', error)
}, }),
),
) )
} else if (data.type === 'cancelNotification') { } else if (data.type === 'cancelNotification') {
event.waitUntil( 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) => { self.addEventListener('notificationclick', (event) => {
console.log('клик на уведомление', event.notification) console.log('клик на уведомление', event.notification)
event.notification.close()
const urlToOpen = new URL(event.notification.data.url, self.location.origin).href
// Открытие URL из данных уведомления // Открытие URL из данных уведомления
event.waitUntil( event.waitUntil(
@@ -44,7 +51,7 @@ self.addEventListener('notificationclick', (event) => {
.then((clientList) => { .then((clientList) => {
for (const client of clientList) { for (const client of clientList) {
console.log('client.url', client.url) 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('фокус') console.log('фокус')
return Promise.all([ return Promise.all([
@@ -58,8 +65,8 @@ self.addEventListener('notificationclick', (event) => {
} }
} }
if (clients.openWindow) { if (clients.openWindow) {
console.log('новое окно') console.log('новое окно', urlToOpen)
return clients.openWindow(event.notification.data.url).then((client) => { return clients.openWindow(urlToOpen).then((client) => {
if (client) { if (client) {
return client.focus() return client.focus()
} }
@@ -67,6 +74,9 @@ self.addEventListener('notificationclick', (event) => {
}) })
} }
}) })
.then(() => {
return event.notification.close()
})
.catch((error) => { .catch((error) => {
console.error(error) console.error(error)
}), }),