在angularjs中使用服务工作者 [英] Using service worker in angularjs
问题描述
以下示例代码正在运行,并在chrome / firefox中显示通知
$ b $ pre $
self.addEventListener('push',function(event){
console.log('[Service Worker] Push Received'。 );
// console.log(`[Service Worker] Push有这样的数据:$ {event.data.text()}`);
console.log(event .data);
console.log(event.data.json());
console.log(typeof(event.data.json()));
console.log(event );
window.dispatchEvent(new Event('dataisthere'));
const title ='YummZ';
const options = {
body:'Message Received \\\
'+ event.data.json()。message,
icon:'images / icon.png',
// badge:'images / badge.png',
data:event.data.json()
};
event.waitUntil(self.registration.showNotification(title,options));
});
我尝试调度窗口事件,但是出现错误窗口未定义
当服务人员得到推送通知时,我需要通知我的角度应用程序执行操作。
注意:新服务工作 -russell.co.uk/2016/01/29/service-worker-messaging.htmlrel =nofollow noreferrer>这个。还没有测试过,但我认为你正在寻找解决方案的总体思路就在那里。标题是如何在服务人员和客户端之间发送消息,所以如果您设法缩小这个差距,那么几乎可以告诉您的服务人员去做任何您想要的。
How we can get event fired in service worker in our angularjs app.
Here is sample code which is working and showing notification in chrome/firefox
self.addEventListener('push', function(event) {
console.log('[Service Worker] Push Received.');
// console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
console.log(event.data);
console.log(event.data.json());
console.log(typeof (event.data.json()));
console.log(event);
window.dispatchEvent( new Event('dataisthere') );
const title = 'YummZ';
const options = {
body: 'Message Received \n ' + event.data.json().message,
icon: 'images/icon.png',
// badge: 'images/badge.png',
data : event.data.json()
};
event.waitUntil(self.registration.showNotification(title, options));
});
I tried to dispatch a window event but i got error window is undefined
when service worker get push notification, i need to notify my angular app to perform action.
NOTE: NEW TO SERVICE WORKER
Have a read on this. Havent tested it yet but I think the general idea for the solution you are looking for is there. The title says How to Send Messages Between Service Workers and Clients, so if you manage to bridge that gap, you can pretty much tell your service worker to do whatever you want.
这篇关于在angularjs中使用服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!