Skip to content

Instantly share code, notes, and snippets.

@orf
Created December 1, 2019 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orf/74b70edae3b2badc12fef6c937b3ec75 to your computer and use it in GitHub Desktop.
Save orf/74b70edae3b2badc12fef6c937b3ec75 to your computer and use it in GitHub Desktop.
'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