Updated March 31, 2023
Definition of React Native Push Notifications
In react, native push notification is a way by which we can send any type of notification to the app, with the help of push notification we can send notifications to multiple users at the same time, and also we can get the response or acknowledgment of the push notification. You must have seen many times that you get a notification on apps for if any new data or change come for example. You are using a react native app for an online class and when the time of class will start you will get an app notification for the class is going to start.
Syntax:
Below is a simple syntax for sending the react-native notification to the react native app. Here firstly we imported the required module of react-native-notifications, this allows us to communicate in the form of the notification on the apps.
import { Notifications } from 'react-native-notifications'
Let rne = () => {
Notifications
events()
registerNotificationReceivedForeground((not, comp) => {
//Statement 1
//Statement 2
}
We can check in the rne as the output returns from the notification.
How do Push Notifications Work in React Native?
Before going to understand the working of the push notification in the react-native we need to understand why we need it. You have seen many time when you are using an app for any shopping or news-related applications that if there is any change in the shopping or any new offers come then they send a notification on your app which allow us to directly redirect to the app by clicking on it, hence previously instead of sending same on the text message people may or may not open the app but in case of push notification by clicking notification it will redirect us to the app which makes the user more connected to us. If we want to set up and send any push notification to any android app or to any IOP app we need to create a project on the google firebase console and we need to register our app there on the firebase console. We can explain the setup and working of it in the following steps.
- Firstly, we need to add the project.
- Then we need to name the project.
- We can turn it off or on for google analytics where we can capture user app notifications and view status.
- After doing all these next we need to register the app to the firebase where we will inform google about our app and they will generate a token.
- Finally, we need to configure the project, so for that, we need to download the google-service.json file and this file needs to be put in the directory your project directory/android/app/folder
We will be able to send a notification to any app which is in use by any user with the help of a firebase console. We first get the unique token for the app inside the server. To get an app unique token when the user will open the first time toke it will generate the token and that token will be stored on the server-side. And when we need to send notifications on the apps we can do it by calling an app with app unique tokens. React native client-side setups,
- Firstly we need to add the library to our application.
- And finally, we need to link our react-native application to the project directory/android/setting.gradle
Examples of React Native Push Notifications
In the below example we have created two components, out of which these components will handle receiving the notification and responding to those coming notifications. We have also given a screenshot of how we can call the apps from the server by providing proper tokens and details of the devices. Please see the below example along with the screen. First, we need to login to the fire base we need to generate the token, you will get json on feeling all details .use url to register https://console.firebase.google.com. In google, firebase go to the cloud messaging section like in the below images. You can get all the details related to creating a token and registering app on the firebase of google for FCM.
Please try to generate your own tokens. These server keys will be an important part of sending notifications. Server-side configuration to get call,
//Install the getstream package and connect with the help of your token and secret you received from google .
let stream = require('getstream');
let cl = stream.connect('Add here apikey you will get from google firebase','Add here your secret from google firebase');
let fd = cl.feed(write the timeline, 'user-1');
fd.addActivity({
student: client.user('user-one').ref(),
work: 'task',
'reason’': 'Sending a greetings',
}})
React side configurations ,
import { StreamApp } from 'react-activity-feed'
//We are Importing the stream app package of react for receiving the notification .
class App extends React.Component {
render() {
return(
<StreamApp
//Please use your own token for notification ,i have taken my google token for notification .
token=""
//Please use your own apiKey
apiKey=""
//Please your appId
appId=""
>
<NotificationDropdown get-notification/>
</StreamApp>
);
}
}
ReactDOM.render(, document.getElementById('main'));
CSS,
Below is the CSS code
body {padding-left: 40px; background: green;}
Html,
Below is the HTML to contains the components
<div id="main"></div>
Screen ,
Below is the screen with my output on the codepen open source.
Advantages
There are multiple advantages of using push notifications, which we can explain with the help of some of the points below.
- It allows us to notify in particular to build apps for any new features or any new offers and updates.
- We are not required to do too much manual work to do setups, it is easy to get set up with the firebase of google.
- It has also ways to manage the error and success returned from the app after sending notifications from the app.
- By sending any silent push notification we can check if the app is deleted or the phone is off.
Recommended Articles
This is a guide to React Native Push Notifications. Here we discuss the definition, syntax, working, and examples of push notifications in react native along with code implementation. You may also have a look at the following articles to learn more –