|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener('push', function(event) { |
|
|
var notificationData = {}; |
|
|
|
|
|
try { |
|
|
notificationData = event.data.json(); |
|
|
} catch (e) { |
|
|
console.log('event.data.json() failed', e); |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
event.waitUntil( |
|
|
clients |
|
|
.matchAll({ |
|
|
type: 'window', |
|
|
includeUncontrolled: true, |
|
|
}) |
|
|
.then(windowClients => { |
|
|
for (let i = 0; i < windowClients.length; i++) { |
|
|
const windowClient = windowClients[i]; |
|
|
|
|
|
|
|
|
if ( |
|
|
windowClient.focused && |
|
|
|
|
|
!(self.registration.scope.indexOf('http://localhost:3000') === 0) |
|
|
) { |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
return self.registration.showNotification(notificationData.title, { |
|
|
vibrate: [200], |
|
|
icon: '/img/apple-icon-144x144-precomposed.png', |
|
|
badge: '/img/badge.png', |
|
|
body: notificationData.body, |
|
|
title: notificationData.title, |
|
|
timestamp: notificationData.timestamp, |
|
|
image: notificationData.image, |
|
|
tag: notificationData.tag, |
|
|
data: notificationData.data, |
|
|
|
|
|
|
|
|
}); |
|
|
}) |
|
|
); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener('notificationclick', function(event) { |
|
|
event.notification.close(); |
|
|
|
|
|
const urlToOpen = |
|
|
event.notification.data && event.notification.data.href |
|
|
? |
|
|
new URL(event.notification.data.href, self.location.origin).href |
|
|
: '/'; |
|
|
|
|
|
|
|
|
event.waitUntil( |
|
|
|
|
|
self.clients |
|
|
.matchAll({ |
|
|
type: 'window', |
|
|
includeUncontrolled: true, |
|
|
}) |
|
|
.then(function(clientList) { |
|
|
|
|
|
if (clientList.length > 0) { |
|
|
return clientList[0] |
|
|
.focus() |
|
|
.then(client => client.navigate(urlToOpen)); |
|
|
} |
|
|
|
|
|
|
|
|
return self.clients.openWindow(urlToOpen); |
|
|
}) |
|
|
); |
|
|
}); |
|
|
|