Send FCM push notifications in a React app using Novu
Create a new Firebase project
Now, we need to add firebase to our app
Choose the web option to connect to a web app
Give your app a name and click on the 'Register' button
Turn on Firebase Cloud Messaging integration from the Novu Dashboard
Click on the 'Add a workflow' button to create workflows
Click on the 'Add a workflow' button to create workflows
Drag and drop the 'Push' node below the Workflow Trigger node
You'll get an error saying 'Message content and title are missing'
title
and body
for the title and the content of notifications, so let’s add them in the ‘Push message title’ and the ‘Push message content’ fields respectively. So, add them and give your workflow a suitable name.
Add the identifiers that you'll use
Click on the 'get snippet' button to get the trigger code
firebase.js
and add the following to it:getToken
method provided by Firebase. But we want to use this only when the user has allowed notifications on their end. So let’s request notification permission from the user and if granted, we’ll generate the token. Add this to your root module:getToken
method, you need a Voluntary Application Server Identification or VAPID key
. Go to your Project Settings -> Cloud Messaging -> Generate key pair (under Web Push Certificates):
Generate a VAPID key from your Project Settings in Firebase Console
firebase-messaging-sw.js
and add the following to it:novu.js
, in our case) and the trigger function is getting called in the controller function above by the name createNotif
.
If you’re following the guide, you should already have the trigger function. But before we can add it to our app, we need one key thing - Subscribers.
Subscribers are entities to which the notifications are sent. You can see a list of subscribers in the Novu dashboard as well.
We’ll create a subscriber in our app. Our backend will be written in Node.js, but we also have backend SDKs in PHP, .NET, Go, Ruby, Python, and Kotlin. The recommended way to create a subscriber in NodeJS is as follows:
subscriberID
of whatever value the env
file contains for the identifier NOVU_SUB_ID
. You can read more about subscribers in our docs..
Back in our app, before we can now add the trigger function, we need to set device identifiers using the setCredential method:
subscriberID
and the deviceTokens
in the
setCredentials
method.