@r3l/app
Version:
29 lines (27 loc) • 954 B
JavaScript
self.addEventListener('push', function(event) {
const payload = event.data ? event.data.json() : {};
event.waitUntil(
self.registration.showNotification(payload.title, { ...payload, data: payload })
);
});
self.addEventListener('notificationclick', function(event) {
const data = event.notification.data;
const url = data.url;
event.notification.close(); // Android needs explicit close.
event.waitUntil(
clients.matchAll({ type: 'window' }).then(windowClients => {
// Check if there is already a window/tab open with the target URL
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
// If so, just focus it.
if (client.url === url && 'focus' in client) {
return client.focus();
}
}
// If not, then open the target URL in a new window/tab.
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});