Learn how to send FCM push notifications in a React native Android app using Novu and FCM
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:
react-native-firebase
package using the following command:
token key
from FCM. We’ll need this to send notifications using FCM. We’re using useEffect
to grab this key as follows:
background handler
to, as the name suggests handle notifications when the app is in the background. So, we’ll add it to our app:
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:
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.
Start the server to run the app.
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'
Add the identifiers that you'll use
Click on the 'get snippet' button to get the trigger code
identify
method:
setCredentials
method:
A notification is successfully received on the device
{{title}}
and {{body}}
in our workflow.
{{title}}
and {{body}}
respectively.
You can send dynamic content in notifications by changing the workflow.
payloadTitle
and payloadBody
from the request bodyA notification with dynamic content is successfully received on the device
URL
of the image in the override as shown below:
A notification with image content is successfully received on the device
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
:
A notification with custom data content is successfully received on the device
A low priority notification is successfully received on the device