Today, we’ll use expo to implement push notification in our simple react native app.
First understands some basics,
What is push notification and why do we need this?
Push notification is an automated message sent by an application to a user when the application is not open. App owners will receive push notifications whenever new content is uploaded.
A text message will bring you to your messaging app, while a push notification will bring the user to whatever app sent the message. Push notifications are much better because the user knows exactly who it’s coming from. It’s clear that the message is from the app that they’ve installed on their device.
What is Expo?
Expo is a set of tools and services for building, deploying, and quickly iterating on native iOS, Android, and web apps from the same codebase.
Now lets dive into the actual implementation and we will understand it step-by-step.
First thing you need to have Expo CLI installed globally on your development machine. You can install it by using npm command :
**npm install -g expo-cli**
After installing Expo CLI create a new project using command:
**expo init ReactNativePushNotification**
Expo CLI will ask you to choose between templates, choose ‘blank’ under the Managed workflow section. Expo CLI is now initializing a new project: it copies a basic template and installs react, react-native and expo. When the project is initialized and ready to go, the command will exit.
When it comes to adding functionality that can access potentially sensitive information on a user’s device, such as their location, or possibly send them unwanted push notifications, you will need to ask the user for their permission first. To do so we need to add Permissions module in our app.
Install Permissions module using below expo cli command,
**expo install expo-permissions**
This will add dependency in your package.json file. Here are the dependencies at the time of building this project:
**"expo": "^35.0.0", "expo-permissions": "~7.0.0", "react": "16.8.3"**
Now that we have permission module installed, we will use it to get NOTIFICATION token from user. For that we first need to ask user to grant permission.
Focus on the function getPushNotificationPermissions . This function asks user to grant notification permission and prints the token on console. If you run this with yarn start , you will see the token printed in you shell console as below.
The token ExponentPushToken[YPU4bUD2NANefVgvBa9ANi] is used to uniquely identify the device and send notification to it.
It’s time to buckle up and get ready to send our first notification. For that we need a tool which will generate a notification request and send it to our app. Expo has provided a tool for that
Open this tool and add below information in the form shown,
Here , you will notice that the Expo Push Token is nothing but the token that we have generated earlier in our app.
Now, keep your app running on your mobile and hit the ‘Send a notification’ button at the bottom of the tool. You will get a notification right on top of your mobile screen. Cool, isn’t it!
We have successfully created a react native app(with Expo) which allows user to receive and display push notifications. You can find the final code in the GitHub repo,
I hope this will help you to understand and encourage you to freely use Expo for with React Native. Please feel free to give feedback and suggestions!