【问题标题】:Chrome Push Notification Updating Service-Worker.jsChrome 推送通知更新 Service-Worker.js
【发布时间】:2017-02-07 10:33:46
【问题描述】:

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

我正面临以下问题。
- 我的用户开始使用通知。
- 我需要更改 service-worker 的逻辑,但无法更新它。
- 我之前的 Service-Wroker.js 没有提供任何基于缓存的安装
- 我没有在之前的 Service-Worker.js 中使用任何 fetch 事件

在新的 Service-Worker.js 中所做的更改
- 登陆 URL (clickUrl) 变量被添加到 self.addEventListener 函数中

我现有的 Service-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();
});

【问题讨论】:

    标签: javascript google-chrome push-notification


    【解决方案1】:

    触发更新:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      相关资源
      最近更新 更多