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.

Closing a notification

Closing a notification - JavaScript Tutorial

From the course: JavaScript: Progressive Web Applications

Start my 1-month free trial

Closing a notification

- Although I glossed over it, it's important to know that the notification is shown from the service worker, not the web app. The service worker runs on it's own thread, independent of the app. That's what allows it to display even when the app isn't active. So it makes sense to handle notification events in the service worker. Let's do the close event first. First, let's add a function that will allow us to track the users interaction with the notification. We'll do that inside SW.JS. So I'll scroll to the bottom and here's my function. I'm gonna call it close notification. And to that, I'm gonna pass a message to display and I'll get an event object from the event listener. And I just wanna write console.log, and we'll pass in whatever's in the message as well as any data stored in the event. So that event.notication.data. And then finally once I've written my message in the data to the console, we need to eventually close the notification. So the notification object stored inside…

Contents