2

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

2
  • @jeff-posnick Any idea? Commented Jun 21, 2017 at 7:21
  • hi, did u find a solution ? pls post back the solution if u have found. Thanks. Commented Feb 3, 2019 at 7:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This 'craig-russell.co.uk/2016/01/29/…' is for Service workers in Javascript... the ? question ? is actually about service worker event handling in Angular JS... Angular 1.x ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.