DEV Community

Cover image for Implement Push Notification with Firebase in React Native
RajanTank for BoTreeTechnologies

Posted on

Implement Push Notification with Firebase in 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. To set up your development machine, follow the official guide here.

We are going to use 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.

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

In build.gradle


In AndroidManifest.xml

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

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

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.

here's our notification arrived

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

Top comments (5)

Collapse
 
1awaleed profile image
1awaleed

A huge RED FLAG here please.

using npm install react-native-firebase installs v5.6.0 which is way way deprecated and react native firebase is at v10+

Please update this article. It was outdated the moment it was posted.

Collapse
 
longman0512 profile image
Smart Developer

Hello
You missed this part.
In android/build.grade file,

dependencies {
classpath("com.android.tools.build:gradle:3.4.1")
classpath 'com.google.gms:google-services:4.2.0'
}

repositories {
google()
maven {
url 'maven.google.com'
}
jcenter()
}

everybody can fine this solution on this url
djamware.com/post/5d7773f05d8cdc05...

Thanks

Collapse
 
muhammadkhan123829 profile image
Muhammadkhan123829

dear sir I got error. I followed all you step by step, as you did above.
Please help me. thanks

Collapse
 
a7med_elsayed_ profile image
Ahmed Elsayed

what is the type of your error ?

Collapse
 
lylest profile image
wackyizzy

Could not find method implementation() for arguments [com.google.firebase:firebase-messaging:20.1.5] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.