Push Notification Using Firebase Cloud Messaging And React Native & Expo | Project And Tutorial | JS
Table of Contents
Introduction
In this tutorial, you will learn how to implement push notifications in a React Native application using Firebase Cloud Messaging (FCM) with Expo. This guide is designed for developers who want to enhance their mobile applications by adding push notification functionality. Note that FCM does not directly support the Expo Go app, so we'll use a development build method.
Step 1: Set Up Firebase for Your Android Project
-
Create a Firebase Project
- Go to the Firebase Console.
- Click on "Add Project" and follow the prompts to create a new project.
-
Add an Android App to Your Firebase Project
- Click on "Add app" and select the Android icon.
- Enter your app's package name (you can find this in your
app.jsonfile). - Download the
google-services.jsonfile provided by Firebase and place it in your project'sandroid/appdirectory.
-
Configure Firebase in Your React Native App
- Install Firebase dependencies using npm or yarn:
npm install @react-native-firebase/app @react-native-firebase/messaging - Ensure you have the required permissions in your
AndroidManifest.xml:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- Install Firebase dependencies using npm or yarn:
Step 2: Create Development Build with Expo
-
Install Expo CLI
- If you haven't already, install Expo CLI globally:
npm install -g expo-cli
- If you haven't already, install Expo CLI globally:
-
Create a New Expo Project
- Use the following command to create a new Expo project:
expo init YourProjectName
- Use the following command to create a new Expo project:
-
Eject to Bare Workflow
- To use Firebase, you need to eject your Expo project to the bare workflow:
expo eject
- To use Firebase, you need to eject your Expo project to the bare workflow:
-
Install Required Packages for Development Build
- Navigate to your project directory and run:
npm install
- Navigate to your project directory and run:
Step 3: Implement Push Notifications
-
Request User Permissions
- In your main component file (e.g.,
App.js), request permission to receive notifications:import messaging from '@react-native-firebase/messaging'; async function requestUserPermission() { const authStatus = await messaging().requestPermission(); const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (enabled) { console.log('Authorization status:', authStatus); } } useEffect(() => { requestUserPermission(); }, []);
- In your main component file (e.g.,
-
Get FCM Token
- Retrieve the FCM token to send push notifications:
useEffect(() => { const unsubscribe = messaging().onTokenRefresh(token => { console.log('New FCM Token:', token); }); return unsubscribe; }, []);
- Retrieve the FCM token to send push notifications:
-
Handling Foreground Notifications
- Set up a listener to handle incoming notifications while the app is in the foreground:
useEffect(() => { const unsubscribe = messaging().onMessage(async remoteMessage => { Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage)); }); return unsubscribe; }, []);
- Set up a listener to handle incoming notifications while the app is in the foreground:
-
Handling Background and Quit State Notifications
- To handle notifications when the app is in the background or quit state, set up the following in your
index.jsorApp.js:messaging().setBackgroundMessageHandler(async remoteMessage => { console.log('Message handled in the background!', remoteMessage); });
- To handle notifications when the app is in the background or quit state, set up the following in your
Conclusion
You have successfully integrated push notifications into your React Native application using Firebase Cloud Messaging and the Expo development build method. Key takeaways include setting up Firebase, creating a development build, and handling foreground and background notifications. For further enhancements, explore features like custom notification sounds and handling different notification types. Happy coding!