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 - JavaScript Tutorial
From the course: JavaScript: Progressive Web Applications
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…
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)
-
-