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