Chrome推送通知正在更新Service-Worker.js

时间:2017-02-07 10:33:47

标签: javascript google-chrome push-notification

我在我的网站中使用Google Chrome推送通知。 目前,我有超过1,00,000个订阅用户。

我正面临以下问题。
- 我的用户开始使用通知 - 我需要改变服务工作者的逻辑,但无法更新它 - 我没有使用我以前的Service-Wroker.js进行任何基于缓存的安装 - 我没有在之前的Service-Worker.js

中使用任何fetch事件

在新的Service-Worker.js中完成的更改
- 着陆网址(clickUrl)变量已添加到 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();
});

1 个答案:

答案 0 :(得分:0)

  

触发更新:

     
      
  • 导航到范围内页面。
  •   
  • 关于推送和同步等功能事件,除非在过去24小时内进行了更新检查。
  •   
  • 仅在服务工作者URL已更改时才调用.register()。
  •   

更多https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle