最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - increase Service Worker life time - Stack Overflow

programmeradmin1浏览0评论

I'm trying push notifications using service workers. use my own server to send notifications and eventSource is used to establish the connection. after i open the webpage service worker is running perfectly. but it stops after few seconds. what i want is to run service worker without getting stopped. i want it to run until the browser is opened. can anyone remend a way to do that. ?

this is my ServiceWorker JS

/**
 * 
 */
'use strict';
var eventSource2 = new EventSource("Servlet");
console.log('Started', eventSource2);
eventSource2.addEventListener('install', function(event) {
    eventSource2.skipWaiting();
    console.log('Installed', event);

});

eventSource2.addEventListener('push',function(event) {
                    console.log(event.data);
                    var title = event.data;
                    self.registration.showNotification(title,{
                                        'body' : event.data,
                                        'icon' : '.png',
                                        'tag' : 'click',
                                    });
                    console.log("data from down", event.data);
                });

eventSource2.addEventListener('activate', function(event){
    console.log('Activated', event);
});

eventSource2.addEventListener('fetch', function(event) {
    console.log('Push message', event);
});

I'm trying push notifications using service workers. use my own server to send notifications and eventSource is used to establish the connection. after i open the webpage service worker is running perfectly. but it stops after few seconds. what i want is to run service worker without getting stopped. i want it to run until the browser is opened. can anyone remend a way to do that. ?

this is my ServiceWorker JS

/**
 * 
 */
'use strict';
var eventSource2 = new EventSource("Servlet");
console.log('Started', eventSource2);
eventSource2.addEventListener('install', function(event) {
    eventSource2.skipWaiting();
    console.log('Installed', event);

});

eventSource2.addEventListener('push',function(event) {
                    console.log(event.data);
                    var title = event.data;
                    self.registration.showNotification(title,{
                                        'body' : event.data,
                                        'icon' : 'http://icons.iconarchive./icons/designbolts/free-multimedia/1024/iMac-icon.png',
                                        'tag' : 'click',
                                    });
                    console.log("data from down", event.data);
                });

eventSource2.addEventListener('activate', function(event){
    console.log('Activated', event);
});

eventSource2.addEventListener('fetch', function(event) {
    console.log('Push message', event);
});
Share Improve this question asked Mar 7, 2016 at 7:04 YM-91YM-91 3111 gold badge5 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Service workers are meant to have a short lifetime, you can't make them run forever.

You could use a shared worker to do that, but they only run if your website is open.

You are trying to mix the push API and EventSource. If you use the Push API, you don't need to keep the service worker alive all the time, you can just make it execute when a push notification arrives.

See the example on the ServiceWorker Cookbook.

When you catch a push like here:

self.addEventListener('push',function(event) {

the first thing to do is to use event.waitUntil() who takes a promise and extends the lifetime of the event and the serviceworker

self.addEventListener('push',function(event) {
  event.waitUntil(<YOUR PROMISE>)
}

This is not possible and it's not the purpose of a service-worker. Why exactly do you want to keep it alive ?

发布评论

评论列表(0)

  1. 暂无评论