From the course: JavaScript: Progressive Web Applications

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Handling push notifications

Handling push notifications - JavaScript Tutorial

From the course: JavaScript: Progressive Web Applications

Start my 1-month free trial

Handling push notifications

- [Instructor] Next we'll need to handle push events in the service worker. This makes sense, as a service worker will be the script started by the browser, if our app isn't active. So let's jump back into NetBeans. And then in the sw.js file, let's add an event listener for push messages. I'm going to do that at the bottom of the file. And the push event listener looks just like all the others. It starts with self.addEventListener. And this time we'll be listening for the push event. And we'll handle it with a function, that gets the event as as a parameter. Punctuation. Okay, let's write a message to ourselves saying that we've received a push message. So console.log. How 'bout something like push message received. And then here is where we can configure our notification. So let's be smart about this. I'm just gonna copy the options object that I commented out in main.js. So that was in the service worker registration. Here's the options object. Let's select it. Copy it. And then…

Contents