chet novoe
This commit is contained in:
39
public/service-worker.js
Normal file
39
public/service-worker.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// public/service-worker.js
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
const data = event.data.json();
|
||||
console.log(data);
|
||||
const options = {
|
||||
body: data.url,
|
||||
icon: "/icon.png",
|
||||
badge: "/badge.png",
|
||||
data: data.url
|
||||
};
|
||||
event.waitUntil(self.registration.showNotification(data.title, options));
|
||||
// console.log(data);
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', function (event) {
|
||||
// console.log("Notification click event received."); // Сообщение об активации обработчика
|
||||
console.log("Notification data:", event.notification.data); // Проверка данных уведомления
|
||||
|
||||
event.notification.close(); // Закрываем уведомление
|
||||
// console.log(event)
|
||||
const targetUrl = event.notification.data && event.notification.data ? event.notification.data : '/';
|
||||
// console.log("Target URL to open:", targetUrl); // Выводим URL, который будет открыт
|
||||
|
||||
event.waitUntil(
|
||||
clients.matchAll({type: 'window', includeUncontrolled: true}).then(clientList => {
|
||||
for (const client of clientList) {
|
||||
if (client.url === targetUrl && 'focus' in client) {
|
||||
// console.log("Focusing on existing client:", client.url); // Сообщение об активации уже открытого клиента
|
||||
return client.focus();
|
||||
}
|
||||
}
|
||||
if (clients.openWindow) {
|
||||
// console.log("Opening new window:", targetUrl); // Сообщение об открытии нового окна
|
||||
return clients.openWindow(targetUrl);
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user