A demo that illustrates how to configure service workers for push notifications in a React app using Google Firebase. This demo was created as part of a presentation about using service workers to add push notifications and offline capabilities to web apps.
To run this app yourself and try the demo, you'll need to do the following:
-
Create a file in the
src
directory calledfirebaseConfig.js
that exports the Firebase config object from the Project Settings screen in the Firebase Console. Make sure that you have anauthDomain
, adatabaseUrl
, and amessagingSenderId
. -
Configure cloud messaging:
- On the Project Settings screen in the Firebase Console, go to the Cloud Messaging tab.
- In the Web Configuration section, create a Web Push certificate.
- Copy the public key and export it from your
firebaseConfig.js
file asfirebaseMessagingPushNotificationsKey
You can run the React app locally to try the demo. It's using React 18.
- Run
npm i
in the root project directory - Run
npm run start
to start the application - Go to
http://localhost:3000
in your browser
You will need to enable push notifications for the localhost URL in your browser in order for the demo to work.
If you decide to host the application with Firebase Hosting, update the .firebaserc
file to reference your project.
The Cloud Function uses Node 18.
To test the Firebase Function that sends the push notifications, you can run the function locally.
Alternatively, the Firebase Function can be deployed to Firebase for testing the push notifications.
- Run
npm install -g firebase-tools
to install the Firebase Tools CLI - Run
firebase login
in your Terminal to login to your Firebase account via the CLI. (You might need to restart your Terminal for the command to be found after running step 1). - In the
functions
directory, runnpm i
to install the packages. - Also in the
functions
directory, runnpm run deploy
to deploy the function to Firebase Cloud Functions
Note: You will need at least the Blaze plan with Firebase to deploy and run the function.