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

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

javascript - Using service worker in angularjs

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...