Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
self.addEventListener('install', function (event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function(event) {
event.waitUntil(clients.claim());
});
self.addEventListener('push', function(event) {
event.waitUntil(
self.registration.pushManager.getSubscription()
.then(function(subscription) {
return fetch('https://texterra.me/?endpoint=' + subscription.endpoint.split('/').pop() + '&ver=2')
.then(function(response) {
return response.json()
.then(function(data) {
return self.registration.showNotification(data.title, data.body);
});
});
})
);
});
self.addEventListener('notificationclick', function(event) {
const target = event.notification.data.url;
event.notification.close();
event.waitUntil(clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then(function(clientList) {
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);
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.