Implement Push Notification with Firebase in React Native 
Tags: react, reactnative, javascript, notification
Published: 2020-05-14 05:43:19

---
description: React native development enables you to implement push notification with firebase. Here's a complete guide on how you can implement it with react native
---

Hello folks. I hope you all are doing well.

In this tutorial, you’re going to learn how to implement push notifications as an app feature using React Native and Firebase.

There are two main ways you can send push notifications to your app users: local and remote. Local notifications are sent from a React Native application, while remote push notifications are sent from the server or a push notification service such as Firebase Cloud Messaging Service (FCM). We will explore both approaches.

## What Are Push Notifications?

Push notification is a small message that users receive on a device. They differ from regular pop-ups because they will appear on the device whether or not the user is currently using the site or app that the notification is tied to. 

Push notifications work for both browsers and apps. For browsers, push notifications can show up no matter what site a user is on, as long as they’ve opted in to push notifications. They work very similarly for apps, however, they can pop up on a mobile device at any time, regardless of whether the user is on the app.

## Prerequisites

This tutorial requires basic knowledge of [React Native Development](https://www.botreetechnologies.com/react-native-development). To set up your development machine, follow the official guide [here](https://facebook.github.io/react-native/docs/0.59/getting-started).

We are going to use [react-native-firebase](https://github.com/invertase/react-native-firebase) to implement push notifications in React-native.

First create a project in your Google Firebase Console https://console.firebase.google.com/

###Adding Firebase to your app

Register your app in Firebase and follow the configuration steps for Android.
Download your google-services.json. Don't forget to place them in the correct folder.

{% gist https://gist.github.com/RajanTank/0b0a37343bc323ad065af1bf0d93f1f8 %}

Also create your React Native project and enter it's directory

```
$ react-native init RNPushNotification
$ cd RNPushNotification
```

Add and link the React Native Firebase package to your app

```
$ npm install react-native-firebase
$ react-native link react-native-firebase

```

###Configuration Android project

```In MainApplication.java```
{% gist https://gist.github.com/RajanTank/f6cd8ff48ef11131ff7d03ba0c7f0471 %}

```In build.gradle```
{% gist https://gist.github.com/RajanTank/2cc7afc1589360cf9be2b0476b0655e7 %}

```In AndroidManifest.xml ```
{% gist https://gist.github.com/RajanTank/9087786328544162957a85128b91bbd5 %}

After everything is set up and configured now, we have to implement the FCM push notification in React Native side using React Native Firebase module. 

##Receive Push Notifications
Now let's code the most awaited part… Receiving Notifications! 😉

Before integrating the code please install the following dependencies:
``` npm install @react-native-community/async-storage```

Before the app can get any notifications, it is crucial to ask permission from users. If the user didn’t allow your app to receive notifications, it can never get any unless the user explicitly changes it from Settings.

So, let's have write below code in `` App.js `` file
{% gist https://gist.github.com/RajanTank/6dd36d48bc1f8557231e73dee6432b91 %}


####Listening Notifications

We are now ready to listen to push notification events. Before proceeding, you should know about different types of notifications supported by Firebase.

* Notification-only messages: These are display messages that are automatically handled by Firebase SDK. Notifications are thrown to the device tray.

* Notification + optional data messages: These are also handled by Firebase SDK. Only difference here is when the user taps on notification, your app receives a payload associated with that notification.

* Data only messages: These types of notifications are handled exclusively by app. No notification is thrown on the device tray unless the app explicitly does so. 

###Read Also: [How React Native Improves Developer Productivity](https://www.botreetechnologies.com/blog/how-react-native-improves-developer-productivity)

After having configured everything correctly we can test directly from the firebase console:

* Go to the Cloud Messaging option in the left pane.
* Click on Send your first message
* Enter your test text, select the Android application to which you want to send the application and click Send.

{% gist https://gist.github.com/RajanTank/fc44cbcf7216400e66d71b8c6e1b1075 %}

{% gist https://gist.github.com/RajanTank/ad43ec56afa992b986cfd22c242490d5 %}

here's our notification arrived 
{% gist https://gist.github.com/RajanTank/037f1b9e65e374b0379b6667a28625c7 %}

There are few points you should know

* When an app is in the background and killed in android and you want to listen to data notification, therefore, you need to implement [Headless JS](https://github.com/invertase/react-native-firebase-docs/blob/master/docs/messaging/receiving-messages.md#2-handle-background-messages) functionality.  

* Android, When app is Not in background or killed and you tab on Notification react-native-firebase library won't be able to catch title and body of notification. It will show undefined therefore you also need to send title and body in data of notification.  

###[Click here to read more about](https://www.botreetechnologies.com/react-native-development)