43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
self.addEventListener('push', function(event) {
|
|
if (event.data) {
|
|
let payload = null;
|
|
try {
|
|
payload = event.data.json();
|
|
} catch (e) {
|
|
payload = { title: "New Notification", body: event.data.text() };
|
|
}
|
|
|
|
const title = payload.title || "Identia Notification";
|
|
const options = {
|
|
body: payload.body || "You have a new alert.",
|
|
icon: payload.icon || "/images/myn.png",
|
|
badge: "/images/myn.png",
|
|
vibrate: [200, 100, 200, 100, 200, 100, 200],
|
|
data: payload.url || "/dashboard.php",
|
|
requireInteraction: true
|
|
};
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(title, options) // show the OS notification
|
|
);
|
|
}
|
|
});
|
|
|
|
self.addEventListener('notificationclick', function(event) {
|
|
event.notification.close();
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(clientList) {
|
|
const urlToOpen = event.notification.data || '/dashboard.php';
|
|
for (let i = 0; i < clientList.length; i++) {
|
|
let client = clientList[i];
|
|
if (client.url.includes(urlToOpen) && 'focus' in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(urlToOpen);
|
|
}
|
|
})
|
|
);
|
|
});
|