Using service worker in angularjs

2019-01-29 06:49发布

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

1条回答
Melony?
2楼-- · 2019-01-29 07:27

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.

查看更多
登录 后发表回答