push notification
2Hats . 4 minutes
January 11, 2019

How to setup Firebase Push Notifications in react native applications?

A lot of new apps are showing up in the market day by day. To get our app noticed in this wide range of apps is very difficult these days. Even if it is done, we need to make sure our users are engaged in the app by timely notifications and they must be updated with new changes in the app. Push notifications play a major role in this area.

Mobile platform provides its own service to set it up on our apps. Firebase Messaging Service (FCM) is a cross-platform messaging service from Google. This blog will show how we can set up this service in our react-native app easily.

React native firebase makes using Firebase with react native simple. It provides all firebase functionalities with simple methods. Push notifications are one of it. Different versions available for different versions of react native. We need to use ‘react native firebase’ version which matches our react native version. We can find it from the following table

3.3.x4.3.x5.x.x
React Native0.50-520.52-55^0.56 – ^0.57
Play Services Android SDK11.8.0 +15.0.1^16.0.1
Firebase iOS SDK4.7.0 +5.3.0^5.10.0 – ^5.11.0

 

Initially, we can start by installing the module to our project by using the following command


npm install --save react-native-firebase

To configure the module and initialize the functionalities, we need a firebase project to set up. To do that 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 settings

 

In the new window click on ‘Add Firebase to your Android app’ for Android apps.

 

Fill out the required fields in the popup. In the form, make sure the package name you are using is the same from our project.

 

After clicking the ‘register app’ button you will see a button to download the configuration file as below.

 

Download the ‘google-services.json’ file to the directory ‘android/app’.

Click ‘next’ and complete the process.

That is all we have to do here. We can continue with the plugin integration now.

We need to link RNFirebase to our project first. It can be done by running the following command in the root directory


react-native link react-native-firebase 

Add the google-services gradle to the dependency of your project in the root level build.gradle file:Next, we need to setup google-service.json. As we already added the file to our project directory we have to add following codes to our project files.

To apply the plugin to our project, we need to add the following to the bottom of the android/app/build.gradle file:

The Firebase modules need to be installed as project dependencies. In the android/app/build.gradle file, add the following:

We need to upgrade our gradle file then. For doing it android/gradle/wrapper/gradle-wrapper.properties, update the gradle URL to gradle-4.4-all.zip

And in an android/build.gradle check that you have google() above the jcenter() as follows

In android/build.gradle update Android build tools to version 3.2.0:

In android/app/build.gradle update all your compile statements to be implementation, e.g.

And finally we need to check android/build.gradle have google() above jcenter() as below

Now we need to setup ‘firebase messaging’ to enable push notification service in our app.By doing the above steps we are done with the integration of firebase to our app.

Add the Firebase Cloud Messaging dependency to android/app/build.gradle:

Add the RNFirebaseMessagingPackage to your android/app/src/main/java/com/[app name]/MainApplication.java:

Add the following to android/app/src/main/AndroidManifest.xml:

That’s it. To run the push notification the basic functions were added to the app. 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

react-native run-android

Now we need to setup backend server to sent notifications. Sample PHP server side code added below.

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

$registrationIds are the token generated by the app when register to push notification.

To explore more functionalities of the module and advanced features, we can get more details from https://github.com/invertase/react-native-firebase.

Need help setting up push notifications? Contact us today. Stepping as a react native agency we look forward to more advanced solutions in the technology and explore more.

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
Aneesh ceo

    Stay In The Loop!

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