Files
hereconnect/apps/frontend/public/service-worker.js
T

100 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-11-21 16:57:05 +02:00
self.addEventListener('install', (event) => {
console.log('install event:', event)
// The promise that skipWaiting() returns can be safely ignored.
self.skipWaiting()
// Perform any other actions required for your
// service worker to install, potentially inside
// of event.waitUntil();
})
2024-11-25 22:58:10 +02:00
self.addEventListener('activate', (event) => {
event.waitUntil(
clients.claim(), // Привязка нового сервис-воркера к текущим клиентам
)
})
2024-11-21 01:03:01 +02:00
self.addEventListener('push', (event) => {
const data = event.data.json()
console.log('Push событие получено:', data.type)
// Отображение уведомления
if (data.type === 'notification') {
event.waitUntil(
2024-11-21 16:51:51 +02:00
self.registration
.showNotification(data.notification.title, data.notification.options)
.then(() => {
2024-11-21 01:03:01 +02:00
console.log('Notification создан', data.notification.options.tag)
2024-11-21 16:51:51 +02:00
})
.catch((error) => {
console.error('Notification ошибка создания', error)
}),
2024-11-21 01:03:01 +02:00
)
} else if (data.type === 'cancelNotification') {
event.waitUntil(
self.registration
.getNotifications({ tag: data.notification.options.tag })
.then((notifications) => {
notifications.forEach((notification) => {
if (
notification.tag === data.notification.options.tag ||
notification.data.payload.tag === data.notification.options.data.payload.tag // удалить, если notification.tag поддерживают все
) {
notification.close()
console.log('Notification удален')
}
})
}),
)
}
})
2024-11-21 16:51:51 +02:00
self.addEventListener('notificationclose', function (event) {
const dismissedNotification = event.notification
console.log('dismissedNotification', dismissedNotification)
})
2024-11-21 01:03:01 +02:00
self.addEventListener('notificationclick', (event) => {
2024-11-21 16:11:23 +02:00
console.log('клик на уведомление', event.notification)
2024-11-21 16:51:51 +02:00
const urlToOpen = new URL(event.notification.data.url, self.location.origin).href
2024-11-21 01:03:01 +02:00
// Открытие URL из данных уведомления
event.waitUntil(
2024-11-21 16:11:23 +02:00
clients
.matchAll({ type: 'window', includeUncontrolled: true })
.then((clientList) => {
for (const client of clientList) {
console.log('client.url', client.url)
2024-11-21 16:51:51 +02:00
if (client.url === urlToOpen && 'focus' in client) {
2024-11-21 16:11:23 +02:00
console.log('фокус')
2024-11-21 01:03:01 +02:00
2024-11-21 16:11:23 +02:00
return Promise.all([
client.focus(),
client.postMessage({
type: 'notificationClick',
tag: event.notification.tag,
payload: event.notification.payload,
}),
])
}
2024-11-21 01:03:01 +02:00
}
2024-11-21 16:11:23 +02:00
if (clients.openWindow) {
2024-11-21 16:51:51 +02:00
console.log('новое окно', urlToOpen)
return clients.openWindow(urlToOpen).then((client) => {
2024-11-25 22:58:10 +02:00
if (!client) {
console.log('новое окно не открылось')
2024-11-21 16:11:23 +02:00
}
})
}
})
2024-11-21 16:51:51 +02:00
.then(() => {
return event.notification.close()
})
2024-11-21 16:11:23 +02:00
.catch((error) => {
console.error(error)
}),
2024-11-21 01:03:01 +02:00
)
})