简体   繁体   English

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

[英]Chrome Push Notification Updating Service-Worker.js

I am using Google Chrome Push Notification in my site. 我在我的网站中使用Google Chrome推送通知。 Currently I have more than 1,00,000 subscribed users. 目前我有超过1,00,000个订阅用户。

I'm facing this following issue. 我正面临以下问题。
- My users started using the notifications. - 我的用户开始使用通知。
- I need to change the logic of the service-worker but could not able update it. - 我需要更改服务工作者的逻辑,但无法更新它。
- I have not given any cache based installation with my previous Service-Wroker.js - 我没有使用我以前的Service-Wroker.js进行任何基于缓存的安装
- I have not used any fetch event with the previous Service-Worker.js - 我没有使用以前的Service-Worker.js的任何fetch事件

Changes done in new Service-Worker.js 在新的Service-Worker.js中完成的更改
- Landing URL (clickUrl) variable is added into the self.addEventListener function - 将着陆URL (clickUrl)变量添加到self.addEventListener函数中

My Existing Service-Wroker.js 我现有的服务-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);
    }
};

The new / updated Service-Worker.js [Changes I need to update / implement] 新的/更新的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();
});

An update is triggered: 触发更新:

  • On navigation to an in-scope page. 导航到范围内页面。
  • On functional events such as push and sync, unless there's been an update check within the previous 24 hours. 关于推送和同步等功能事件,除非在过去24小时内进行了更新检查。
  • On calling .register() only if the service worker URL has changed. 仅在服务工作者URL已更改时调用.register()。

more here https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle 更多信息,请访问https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM