From the course: JavaScript: Progressive Web Applications

Unlock the full course today

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

Requesting permission to show notifications

Requesting permission to show notifications - JavaScript Tutorial

From the course: JavaScript: Progressive Web Applications

Start my 1-month free trial

Requesting permission to show notifications

- [Voiceover] Before we can display notifications, the user must first grant us permission. Fortunately, asking for permission is relatively straight forward. Because we eventually will call show notification method, on the service worker registration object, we need to wait until the promise that registers the service worker fulfills. Let's open main.js and add the code just below the line that writes the service workers scope to the console. That will ensure both service workers and notifications are supported. So if main.js isn't open, you can double click it in the js folder. Then move your cursor into the service worker registration block, and find where we wrote the scope of the service worker to the console, then you can hit enter and give yourself some room to work. So the first thing to do is to make sure that notifications are supported. That we can do in a simple conditional statement. So I'm gonna write if. So look for the notification object inside the window object. And…

Contents