39 lines
1.0 KiB
JavaScript
39 lines
1.0 KiB
JavaScript
self.addEventListener('push', function (event) {
|
|
let data = {};
|
|
|
|
try {
|
|
data = event.data?.json() ?? {};
|
|
} catch (e) {
|
|
data = { title: 'Notification', body: event.data?.text() ?? '' };
|
|
}
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title, {
|
|
body: data.body,
|
|
icon: data.icon,
|
|
badge: data.badge,
|
|
data: data.data,
|
|
actions: data.actions,
|
|
}),
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', function (event) {
|
|
event.notification.close();
|
|
|
|
const url = event.notification.data?.url ?? '/';
|
|
|
|
event.waitUntil(
|
|
self.clients
|
|
.matchAll({ type: 'window', includeUncontrolled: true })
|
|
.then(function (clientList) {
|
|
for (const client of clientList) {
|
|
if (client.url === url && 'focus' in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
return self.clients.openWindow(url);
|
|
}),
|
|
);
|
|
});
|