2Hats . 5 minutes

Push notifications in React-native android app using FCM

For a successful mobile app, push notifications are very essential. It keeps the app live and boosts app usage and traffic. Push notifications can keep users updated and give app admin a way to send notification easily to end users and keep them updated. For a beginner, it is always a bit tricky to set up. Sometimes version mismatches could create a lot of error while developing. This article will show step by step implementation of FCM (Firebase Cloud Messaging) notifications to a React-native app.

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution from Google that lets us deliver messages to devices.

First of all, let’s start with a new react native app using the following command. We are not going to all details of setting up the platform for this and believe you have it covered already.

create-react-nataive-pp reactFcm

After the project is created, we have to eject the app to continue the process. As we have to add FCM plugin, we need to modify android core project files to make it work. Eject process will generate the required files to the project. We can eject the app using the following command.

npm run eject

Ejection process will ask for project name which should appear on project files. Enter the required information and complete the ejection process.

Before we start adding FCM plugin, we need to check the versions of our project and android SDK versions. There are some compatibility issues with older versions of react native, matching version details can be found on this link – https://github.com/react-community/create-react-native-app/blob/master/VERSIONS.md

For this tutorial, we are using react native 0.52.x and android sdk 25.0.0. If you are using any other version of react native, please make sure to use compatible versions of Android SDK that is available in the system.

Check/change following on the file app/build.gradle file

 

Run the app after, to make sure everything works as expected.

Now we can add the FCM plugin to the app by running following command.

npm install react-native-fcm --save

After installing the package, run below command to link to react native app

react-native link react-native-fcm

Edit android/build.gradle file as following

 

Edit android/app/build.gradle and add following to the beginning of the file and remaining to required positions

 

Edit android/app/src/main/AndroidManifest.xml:

 

After the above steps completed, we need to create a new project in firebase. Login to firebase  (https://console.firebase.google.com) using your Google account.

Click the ‘add project’ button to create a new project in firebase.

 

 

 

 

 

 

 

 

 

Enter the name of your project and click ‘create project’

 

 

 

 

 

 

 

 

 

 

 

 

 

Go to project settings by clicking the gear icon next to menu called ‘project overview’

 

 

 

 

 

 

 

 

 

 

 

 

 

In the general tab, click ‘add app’ button and create a new app. In the form, make sure the package name you are using is the same from our project.

 

 

 

 

 

 

 

 

 

 

 

Click ‘register app’ and in the next window, download the ‘google-services.json’ file to the directory ‘android/app’.

Done! After these steps, we have successfully completed all the required steps for the integration. Now we can add code to our project to handle notifications in our app.

First, create a file to declare global variables

app/FirebaseConstants.js

 

‘YOUR_API_KEY’ Must be replaced with app’s API key, which we can get from firebase console by going to ‘cloud messaging’ tab in the firebase console and copy the ‘Sender ID’.

 

 

 

 

 

 

 

 

 

 

 

Add following code to the root file of your react native project (app.js/index.andoid.js) to register for push notifications.

Save the generated token to send push notifications to the device

Now we need to handle the push notifications to show up in our application. Initiate following code in the file to handle incoming push notifications.

That’s it. We successfully added basic functions to the app to run push notifications. Please note that push notifications only work on real devices. So, we need to run the app in the connected device using the following command

npm run android

Refer the following repo for more details about the plugin and more sample codes.

https://github.com/evollu/react-native-fcm

To send a notification to the app, we can use the following sample code in PHP.

Replace ‘Your_api_key’ with ‘Legacy server key’  from firebase console – ‘cloud messaging’ tab.

$registrationIds are the token we generated and saved from the app when register to push notification.

Related Article: Push notifications in the Ionic android mobile app using FCM
                         How to setup Firebase Push Notifications in react native applications?

If you need any help with implementing push notifications in your React-Native apps please contact us.

 

 

 

 

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Arsha Content writer

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.