50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
self.addEventListener('push', function (event) {
|
|
if (!(self.Notification && self.Notification.permission === 'granted')) {
|
|
return;
|
|
}
|
|
|
|
let data = {};
|
|
if (event.data) {
|
|
try {
|
|
data = event.data.json();
|
|
} catch (e) {
|
|
data = { title: 'QuitTraq', body: event.data.text() };
|
|
}
|
|
}
|
|
|
|
const title = data.title || 'QuitTraq Update';
|
|
const options = {
|
|
body: data.body || 'Check in on your progress!',
|
|
icon: '/icon-192.png',
|
|
badge: '/icon-192.png',
|
|
tag: data.tag || 'generic-notification',
|
|
data: data.data || {},
|
|
requireInteraction: true
|
|
};
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(title, options)
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', function (event) {
|
|
event.notification.close();
|
|
|
|
// This looks to see if the current is already open and focuses if it is
|
|
event.waitUntil(
|
|
clients.matchAll({
|
|
type: "window"
|
|
})
|
|
.then(function (clientList) {
|
|
for (var i = 0; i < clientList.length; i++) {
|
|
var client = clientList[i];
|
|
if (client.url == '/' && 'focus' in client)
|
|
return client.focus();
|
|
}
|
|
if (clients.openWindow) {
|
|
return clients.openWindow('/');
|
|
}
|
|
})
|
|
);
|
|
});
|