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 - JavaScript Tutorial
From the course: JavaScript: Progressive Web Applications
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Introduction to notifications2m 21s
-
(Locked)
Requesting permission to show notifications5m 26s
-
(Locked)
Displaying a notification4m 50s
-
(Locked)
Adding data to a notification4m 36s
-
(Locked)
Closing a notification3m 33s
-
(Locked)
Handling the notification click event11m 25s
-
(Locked)
Push messaging overview1m 24s
-
(Locked)
Adding Firebase Cloud Messaging to the project2m 15s
-
(Locked)
Subscribing to push messaging6m
-
(Locked)
Handling push notifications3m 14s
-
(Locked)
Sending a tickle using cURL4m 57s
-
(Locked)
Sending data in a push message2m 9s
-
(Locked)
Installing Mozilla's Web Push library2m 51s
-
(Locked)
Modifying the push event handler2m 52s
-
(Locked)
Modifying the notification click event3m 48s
-
(Locked)
Creating the web push script6m
-
(Locked)
Testing the hosted app5m 7s
-
(Locked)
-
-