Hello,
To use React Native Firebase in an Expo app, you need to follow these steps:
Set up a Firebase project:
Go to the Firebase console (https://console.firebase.google.com/) and create a new project.
Configure your Android app within the Firebase project by providing the package name of your Expo app.
Download the google-services.json file and keep it handy.
Install the necessary packages:
Run the following command in your Expo project directory to install the required packages:
expo install react-native-firebase@v5
Configure the Firebase SDK:
Create a file named firebase.js (or any name you prefer) in your project’s root directory.
Inside the firebase.js file, add the following code:
import firebase from ‘react-native-firebase’;
const config = {
// Add your Firebase configuration here
// apiKey: ‘<YOUR_API_KEY>’,
// authDomain: ‘<YOUR_AUTH_DOMAIN>’,
// databaseURL: ‘<YOUR_DATABASE_URL>’,
// projectId: ‘<YOUR_PROJECT_ID>’,
// storageBucket: ‘<YOUR_STORAGE_BUCKET>’,
// messagingSenderId: ‘<YOUR_MESSAGING_SENDER_ID>’,
// appId: ‘<YOUR_APP_ID>’,
};
firebase.initializeApp(config);
export default firebase;
Uncomment and fill in the necessary Firebase configuration values (apiKey, authDomain, etc.) from your Firebase project.
Configure the Android project:
In your Expo project, navigate to android/app/build.gradle.
Add the following lines at the bottom of the file:
apply plugin: ‘com.google.gms.google-services’
In the same directory, open android/build.gradle.
Add the following classpath to the dependencies:
dependencies {
// other dependencies…
classpath ‘com.google.gms:google-services:4.3.10’
}
Add the google-services.json file:
Copy the google-services.json file you downloaded from the Firebase console to the android/app directory of your Expo project.
Test the Firebase setup:
In any component where you want to use Firebase, import the firebase module:
import firebase from ‘…/path/to/firebase.js’;
You can now use Firebase features like Firebase Cloud Messaging (FCM) in your Expo app using the firebase module. For example, to initialize FCM, you can use:
const messaging = firebase.messaging();
// Use messaging instance to subscribe, listen to events, etc.