Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 
 ' + 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
526 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...