Learn how to send FCM push notifications in a React native Android app using Novu and FCM
To complete this tutorial, you will need the following:
Push notifications are notifications that are sent to a user’s devices whether the user is using the app or not. We’ll be using the Firebase Cloud Messaging (FCM) integration of Novu to send these notifications. You can find the complete code for this project in our Github repo.
We’ll use the create-expo
tool to initialize a new expo app. It creates a new React Native app with the expo package installed. We’re naming our app fcm-novu-rn2
and hence using the following command:
Now, go to the project directory and install the react-native-firebase
package using the following command:
Once this is installed, install the messaging module of Firebase using the following command:
Now, we need to grab the token key
from FCM. We’ll need this to send notifications using FCM. We’re using useEffect
to grab this key as follows:
FCM uses something called a background handler
to, as the name suggests handle notifications when the app is in the background. So, we’ll add it to our app:
Similarly, we also need a foreground handler
to handle notifications when the app is in the foreground. In our case, we’re updating a state’s value in our handler. Here’s our foreground handler:
In order to load projects from Expo CLI, we need a dev client to be installed in our app. To install it, execute the following command from the root of the project directory:
Once installed, log into your expo account using the following command:
It’ll ask for your username and password and log you in.
Now, we are ready to create our build of the app. Use the following command to create a development build of the app for the Android platform:
If expo asks if you want to automatically create an EAS project for your app, choose yes. Do the same when it asks you if you want to generate a new keystore.
Do not change the Android package name, even if EAS asks you to!
Create a new Firebase project
Now, we need to add firebase to our app
Choose the web option to connect to a web app
app.json
file of the project:
Find the package name from the `app.json` file.
Add package name to Firebase.
SHA1
keys from our project to the Firebase app. Run the following command to obtain SHA1
key. We’ll get two SHA1
keys, one of which we’ll add to Firebase right now and the other one, later on.Run the given command to obtain `SHA1` key.
SHA1
key and add it to Firebase and register the app:Add the `SHA1` key to the Firebase app.
We'll add the second `SHA1` key now.
Scroll till you see the `SHA1` key you added earlier.
SHA1
key here.Add the second `SHA1` key.
app.json
file:Specify the location of Google services file in the `app.json` file.
Find the server key in the project settings.
If you have an Android device with you, simply scan and install the app. Otherwise, if you want to run it on an emulator, simply copy the link given alongside the QR code and open it on the emulator and then install the app.
Start the server to run the app, using the following command:
Again, you can either scan the QR code or copy and paste the link.
Start the server to run the app.
Sometimes it doesn’t fire up the emulator even when you have it installed. In that case, start the emulator manually and run the server again!
We’ll need a workflow to trigger notifications to our user’s devices. Follow the steps below to create one to ensure we are on the path to making FCM work as expected:
Turn on Firebase Cloud Messaging integration from the Novu Dashboard
If you’re doing this for the first time, you’ll need to get your service account key from the Firebase Console. Read more about it in our FCM provider docs.
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'
Add the identifiers that you'll use
Click on the 'get snippet' button to get the trigger code
A subscriber is essentially the recipient of the notifications sent through Novu’s system. When you create a subscriber, you’re setting up the necessary information for Novu to send targeted notifications to. You can find more about subscribers in our docs.
To create a subscriber, we’ll use the identify
method:
Now, we’ll add the FCM token that we’d received earlier to this subscriber using the setCredentials
method:
To send a notification with Novu, we are actually triggering a notification workflow. Here’s how to do it using Node:
When triggered using the trigger code from above, this is the notification received on the device:
A notification is successfully received on the device
So far, we’ve sent notifications that have had “hard-coded” content. Now we’ll send dynamic data when triggering a notification.
The way to do so is by changing the values of the title and the body to {{title}}
and {{body}}
in our workflow.
{{title}}
and {{body}}
respectively.
You can send dynamic content in notifications by changing the workflow.
We’re extracting payloadTitle
and payloadBody
from the request body
Now, the notification delivered to the subscriber will contain dynamic data as shown below:
A notification with dynamic content is successfully received on the device
Up to this point, your notifications have all contained only text. But if you’ve received many notifications, you know that notifications can have rich content, such as images. It’d be great if your notifications showed users a nice image related to their content. Once again, Firebase makes this super simple.
All you need to do is to specify the URL
of the image in the override as shown below:
The following notification is what gets delivered to the subscriber:
A notification with image content is successfully received on the device
With FCM, you can send Notification
and Data
messages. We have handled sending notification messages. Let’s handle data messages. Data type messages are useful when, in addition to the usual data, you want to send some custom data. Maybe whether a subscriber has purchased the pro plan of our product or not, maybe which tier of usage they have purchased etc. You can do this easily by modifying the override. Here’s an example of sending the subscription status using the identifier subStatus
:
On the user’s device, you can extract this out and have it displayed like this:
A notification with custom data content is successfully received on the device
FCM tries to deliver high-priority messages immediately. The priority of a notification can also be altered by changing the overrides, as shown below:
A low priority notification is successfully received on the device