Javascript Chrome推送通知更新Service-Worker.js
我在我的网站上使用Google Chrome推送通知。 目前我有超过10万的订阅用户 我面临以下问题。Javascript Chrome推送通知更新Service-Worker.js,javascript,google-chrome,push-notification,Javascript,Google Chrome,Push Notification,我在我的网站上使用Google Chrome推送通知。 目前我有超过10万的订阅用户 我面临以下问题。 -我的用户开始使用通知。 -我需要更改服务工作者的逻辑,但无法更新它。 -我以前的服务Wroker.js没有进行任何基于缓存的安装 -我没有对以前的Service-Worker.js使用任何获取事件 在new Service Worker.js中所做的更改 -登录URL(单击URL)变量添加到self.addEventListener函数中 我现有的服务Wroker.js 'use stric
-我的用户开始使用通知。
-我需要更改服务工作者的逻辑,但无法更新它。
-我以前的服务Wroker.js没有进行任何基于缓存的安装
-我没有对以前的Service-Worker.js使用任何获取事件 在new Service Worker.js中所做的更改
-登录URL(单击URL)变量添加到self.addEventListener函数中 我现有的服务Wroker.js
'use strict';
var port;
var pushMessage;
var clickUrl;
var imgUrl;
self.addEventListener('push', function(event) {
var obj = event.data;
pushMessage = event.data ? event.data.text() : '';
var pushData = pushMessage.split('####');
clickUrl = pushData[2];
imgUrl = pushData[1];
if (port) {
port.postMessage(pushMessage);
}
event.waitUntil(self.registration.showNotification(pushData[3], {
requireInteraction: true,
body: pushData[0],
icon: pushData[1]
}));
});
self.addEventListener('notificationclick', function(event) {
if (Notification.prototype.hasOwnProperty('data')) {
event.notification.close();
event.waitUntil(clients.openWindow(clickUrl));
}
});
self.onmessage = function(e) {
port = e.ports[0];
if (pushMessage) {
port.postMessage(pushMessage);
}
};
新的/更新的Service-Worker.js[我需要更新/实施的更改]
'use strict';
var port;
var pushMessage;
var clickUrl;
var imgUrl;
self.addEventListener('push', function(event) {
var obj = event.data;
pushMessage = event.data ? event.data.text() : '';
var pushData = pushMessage.split('####');
clickUrl = pushData[2];
imgUrl = pushData[1];
if (port) {
port.postMessage(pushMessage);
}
event.waitUntil(self.registration.showNotification(pushData[3], {
requireInteraction: true,
body: pushData[0],
icon: pushData[1],
data:{
url : clickUrl
}
}));
});
self.addEventListener('notificationclick', function(event) {
var landingUrl = event.notification.data.url;
if (Notification.prototype.hasOwnProperty('data')) {
event.notification.close();
event.waitUntil(clients.openWindow(landingUrl));
}
});
self.onmessage = function(e) {
port = e.ports[0];
if (pushMessage) {
port.postMessage(pushMessage);
}
};
self.addEventListener('install', function(event) {
console.log('[ServiceWorker] Installed version', version);
event.waitUntil(
caches.open('my-cache').then(function(cache) {
// Important to `return` the promise here to have `skipWaiting()`
// fire after the cache has been updated.
return cache.addAll([/* file1.jpg, file2.png, ... */]);
}).then(function() {
// `skipWaiting()` forces the waiting ServiceWorker to become the
// active ServiceWorker, triggering the `onactivate` event.
// Together with `Clients.claim()` this allows a worker to take effect
// immediately in the client(s).
return self.skipWaiting();
})
);
});
// Activate event
// Be sure to call self.clients.claim()
self.addEventListener('activate', function(event) {
// `claim()` sets this worker as the active worker for all clients that
// match the workers scope and triggers an `oncontrollerchange` event for
// the clients.
return self.clients.claim();
});
将触发更新:
- 在导航到范围内页面时
- 在功能事件(如推送和同步)上,除非在前24小时内进行了更新检查
- 仅当服务工作者URL已更改时才调用.register()
- 在导航到范围内页面时
- 在功能事件(如推送和同步)上,除非在前24小时内进行了更新检查
- 仅当服务工作者URL已更改时才调用.register()