self.addEventListener('push', function (event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
const sendNotification = body => {
let data = JSON.parse(body);
return self.registration.showNotification(data.title, {
body: data.body,
icon: data.icon,
data: {click_action: data.click_action}
});
};
if (event.data) {
const message = event.data.text();
event.waitUntil(sendNotification(message));
}
});
self.addEventListener('notificationclick', function (event) {
const target = event.notification.data.click_action || '/';
event.notification.close();
// This looks to see if the current is already open and focuses if it is
event.waitUntil(clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then(function (clientList) {
// clientList always is empty?!
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url === target && 'focus' in client) {
return client.focus();
}
}
return clients.openWindow(target);
}));
});
Wordpress contect selection blocker plugin by JaspreetChahal.org