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.

Displaying a notification

Displaying a notification - JavaScript Tutorial

From the course: JavaScript: Progressive Web Applications

Start my 1-month free trial

Displaying a notification

- [Instructor] Let's show a simple notification to the user. First, let's add a notify function to main.js. I'm going to do that just beneath the conditional that registers a service worker. So I'll put that there, and then, scroll to the top, add a function, which I'll call notify and it will take a title parameter which will be the title of our notification. So the first thing to do is to make sure that notification permission is granted. We can do that in a simple conditional. So I'll say if notification.permission is granted. So notification dot permission is granted. Next the service worker registration object has a ready property that returns a promise and passes the registration to the handler. So let's write that line next. So, navigator.serviceworker.ready and that of just makes sure that the service worker is ready to show a notification, but to that we can chain a then call and pass in a handler which gets the registration. And I'm going to do the punctuation first and then…

Contents