Skip to main content

Implement Firebase in React-Native



Welcome to React Native Firebase! To get started, you must first setup a Firebase project and install the "app" module.

I  Installation

Installing React Native Firebase requires a few steps; installing the NPM module, adding the Firebase config files & rebuilding your application.

1. Install via NPM

Install the React Native Firebase "app" module to the root of your React Native project with NPM or Yarn:

# Using npm
npm install --save @react-native-firebase/app

# Using Yarn
yarn add @react-native-firebase/app

The @react-native-firebase/app module must be installed before using any other Firebase service.

2. Android Setup

To allow the Android app to securely connect to your Firebase project, a configuration file must be downloaded and added to your project.

Generating Android credentials

On the Firebase console, add a new Android application and enter your projects details. The "Android package name" must match your local projects package name which can be found inside of the manifest tag within the /android/app/src/main/AndroidManifest.xml file within your project.

The debug signing certificate is optional to use Firebase with your app, but is required for Dynamic Links, Invites and Phone Authentication. To generate a certificate run cd android && ./gradlew signingReport and copy the SHA1 from the debug key. This generates two variant keys. You can copy the 'SHA1' that belongs to the debugAndroidTest variant key option.

Download the google-services.json file and place it inside of your project at the following location: /android/app/google-services.json.

Configure Firebase with Android credentials

To allow Firebase on Android to use the credentials, the google-services plugin must be enabled on the project. This requires modification to two files in the Android directory.

First, add the google-services plugin as a dependency inside of your /android/build.gradle file:

buildscript {
  dependencies {
    // ... other dependencies
    classpath 'com.google.gms:google-services:4.2.0'
    // Add me --- /\
  }
}

Lastly, execute the plugin by adding the following to your /android/app/build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services' // <- Add this line

3. iOS Setup

To allow the iOS app to securely connect to your Firebase project, a configuration file must be downloaded and added to your project.

Generating iOS credentials

On the Firebase console, add a new iOS application and enter your projects details. The "iOS bundle ID" must match your local project bundle ID. The bundle ID can be found within the "General" tab when opening the project with Xcode.

Download the GoogleService-Info.plist file.

Using Xcode, open the projects /ios/{projectName}.xcodeproj file (or /ios/{projectName}.xcworkspace if using Pods).

Right click on the project name and "Add files" to the project, as demonstrated below:

Add files via Xcode
Add files via Xcode

Select the downloaded GoogleService-Info.plist file from your computer, and ensure the "Copy items if needed" checkbox is enabled.

Select 'Copy Items if needed'
Select 'Copy Items if needed'

Configure Firebase with iOS credentials

To allow Firebase on iOS to use the credentials, the Firebase iOS SDK must be configured during the bootstrap phase of your application.

To do this, open your /ios/{projectName}/AppDelegate.m file, and add the following:

At the top of the file, import the Firebase SDK:

#import <Firebase.h>

Within your existing didFinishLaunchingWithOptions method, add the following to the top of the method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Add me --- \/
  if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
  }
  // Add me --- /\
  // ...
}

4. Autolinking & rebuilding

Once the above steps have been completed, the React Native Firebase library must be linked to your project and your application needs to be rebuilt.

Users on React Native 0.60+ automatically have access to "autolinking", requiring no further manual installation steps. To automatically link the package, rebuild your project:

# Android apps
npx react-native run-android

# iOS apps
cd ios/
pod install --repo-update
cd ..
npx react-native run-ios

Once successfully linked and rebuilt, your application will be connected to Firebase using the @react-native-firebase/app module. This module does not provide much functionality, therefore to use other Firebase services; each of the modules for the individual Firebase services need installing separately.

Note: Whn Build successfully it connect with firebase IN next Blog we Implement Push-Notification in React-native by firebase

Comments

Popular posts from this blog

Why Use React Native for Your Mobile App Devlopment?

React Native the same code is used for deployment both on iOS and Android platforms.  It means that it’s possible to get an app for two platforms at once — iOS and Android. React Native shortens the development cycle, makes it possible to deliver products in the fastest way. Development of apps is adapted to the hybrid environment and has native results. The framework uses famous ReactJS UI library developed by Facebook for user interfaces and apps creating and implements ReactJS under the hood. It transfers virtual DOM, improved app performance, and more simple programming processes from ReactJS. A built mobile app is smoother and is loaded much faster than a classic hybrid one. As JavaScript interacts asynchronously with the native environment, UI feels fluid and is highly responsive. A single code base is deployed to multiple mobile operating systems. Components are reused anytime at any level into existing code without you rewriting it and recompiling the app.

How to use Axios In React-native

  Axios Is used For Api Calling in React-native ... Step 1.  npm install axios; After installing package. import axios from "axios"; const options = {         method:method   // post or get         headers:{'content-type':'application/json'},         data:data,         url:"Apiurl",     };     axios(options).then(res => {             //Do something     } Note If u Want to Send Form-data then  change Content-type to multiformdata and pass formdata in data field in Options

React Native CLI Vs Expo what to use?

React Native CLI  vs Expo :New React Native Apps. If you’ve decided that React Native is right for your project, this article is for you! We will be cover your options for bootstrapping your app, and the pros and cons for each option. There are two methods for initializing and developing your app: Expo, and the React Native CLI. Until recently there was a distinct third method, using Create React Native App (CRNA). CRNA has since been merged with the Expo project, and only continues to exist as a separate entity to provide backwards compatibility. Expo falls into the first category of tools, providing a more robust and developer friendly development workflow at the cost of some flexibility. Apps bootstrapped with Expo also have access to a slew of useful features provided by the Expo SDK, like  BarcodeScanner ,  MapView ,  ImagePicker , and so many more. Initializing an app with the React Native CLI, via the  react-native init  command provides flexibility ...