Вячеслав Шебанов
Вячеслав Шебанов
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/serviceworker.js')
.then(function(registration) {
// Регистрация SW прошла успешно
}).catch(function(err) {
// Не получилось зарегестрировать SW
});
}
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
return cache.addAll(urlsToCache);
}));
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request, {
cacheName: CACHE_NAME
}));
});
regstate.pushManager.getSubscription()
.then(subscription => {
if (!subscription) {
return regsgate.pushManager
.subscribe({userVisibleOnly: true});
}
return subscription; });
subscription = {
endpoint: "https://url/subId"
}
self.addEventListener("push", function(ev) {
ev.waitUntil(sendNotification()));
});
self.addEventListener('notificationclick',
function(event) {
event.waitUntil(focusTab());
});
navigator.serviceWorker.ready.then(function(registration) {
registration.sync.register({
tag: 'outbox' // default: ''
});
});
self.addEventListener('sync', function(event) {
if (event.registration.tag == 'outbox') {
event.waitUntil(sendEverythingInTheOutbox());
}
});
navigator.serviceWorker.ready.then(function(registration) {
registration.periodicSync.register({
tag: 'get-latest-news',
minPeriod: 12 * 60 * 60 * 1000,
powerState: 'avoid-draining',
networkState: 'avoid-cellular' });
});