Adding Firebase to our service worker
Our goal for the rest of the chapter is to integrate Firebase into our service worker so that it is ready to receive push notifications and display them.
This is a big project. We won't be able to actually display push notifications until the end of the next chapter. However, here we'll see how to integrate third-party services into service workers, and dive a little more into the theory behind service workers.
Naming our service worker
The service we will use to send push notifications to user devices is called Firebase Cloud Messaging, or FCM. FCM works on the web by looking for a service worker and then sending it a message (containing the notification details). The service worker then displays the notification.
By default, FCM looks for a service worker named firebase-messaging-sw.js
. You can change that using firebase.messaging().useServiceWorker
and passing a service worker registration object. For our purposes, however, it will be more straightforward...