YM-*_*-91 3 javascript addeventlistener push-notification server-sent-events service-worker
我正在尝试使用服务工作者的推送通知.使用我自己的服务器发送通知,eventSource用于建立连接.打开网页后,服务工作人员运行得很好.但它在几秒钟后停止.我想要的是运行服务工作者而不会停止.我希望它一直运行,直到浏览器打开.谁能推荐一种方法来做到这一点.?
这是我的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.com/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);
});
Run Code Online (Sandbox Code Playgroud)
服务工作者的寿命很短,你不能让他们永远奔跑.
您可以使用共享工作人员执行此操作,但只有在您的网站处于打开状态时才会运行.
您正在尝试混合推送API和EventSource.如果使用Push API,则无需始终使服务工作者保持活动状态,只需在推送通知到达时执行即可.
请参阅ServiceWorker Cookbook上的示例.
归档时间: |
|
查看次数: |
1411 次 |
最近记录: |