2017-03-02 12:50:09 +00:00
# Android Installation
2017-04-27 11:18:12 +00:00
## 1) Setup google-services.json
2017-04-06 12:01:18 +00:00
Download the `google-services.json` file provided by Firebase in the _Add Firebase to Android_ platform menu in your Firebase configuration console. This file should be downloaded to `YOUR_PROJECT/android/app/google-services.json` .
2017-03-02 12:50:09 +00:00
2017-04-06 12:01:18 +00:00
Next you'll have to add the google-services gradle plugin in order to parse it.
Add the google-services gradle plugin as a dependency in the *project* level build.gradle
`android/build.gradle`
2017-08-26 06:04:32 +00:00
```groovy
2017-04-06 12:01:18 +00:00
buildscript {
// ...
dependencies {
// ...
2017-10-04 15:09:29 +00:00
classpath 'com.google.gms:google-services:3.1.1'
2017-04-06 12:01:18 +00:00
}
}
2017-03-02 12:50:09 +00:00
```
2017-04-06 12:01:18 +00:00
In your app build.gradle file, add the gradle plugin at the VERY BOTTOM of the file (below all dependencies)
`android/app/build.gradle`
2017-08-26 06:04:32 +00:00
```groovy
2017-04-06 12:01:18 +00:00
apply plugin: 'com.google.gms.google-services'
2017-03-02 12:50:09 +00:00
```
2017-04-27 11:18:12 +00:00
## 2) Link RNFirebase
2017-03-02 12:50:09 +00:00
2017-05-30 15:16:56 +00:00
RNFirebase is split into separate modules to allow you to only include the Firebase functionality that you need in your application.
2017-07-20 10:02:31 +00:00
First add the project path to `android/settings.gradle` :
2017-08-26 06:04:32 +00:00
```groovy
2017-07-20 10:02:31 +00:00
include ':react-native-firebase'
project(':react-native-firebase').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-firebase/android')
```
Now you need to include RNFirebase and the required Firebase dependencies in our `android/app/build.gradle` so that they are compiled as part of React Native. In the `dependencies` listing, add the appropriate `compile` lines:
2017-08-26 06:04:32 +00:00
```groovy
2017-07-20 10:02:31 +00:00
dependencies {
// RNFirebase required dependencies
compile(project(':react-native-firebase')) {
transitive = false
}
2017-10-04 15:09:29 +00:00
compile "com.google.firebase:firebase-core:11.4.2"
2017-07-20 10:02:31 +00:00
// If you are receiving Google Play API availability issues, add the following dependency
2017-10-04 15:09:29 +00:00
compile "com.google.android.gms:play-services-base:11.4.2"
2017-07-20 10:02:31 +00:00
// RNFirebase optional dependencies
2017-10-04 15:09:29 +00:00
compile "com.google.firebase:firebase-ads:11.4.2"
compile "com.google.firebase:firebase-auth:11.4.2"
compile "com.google.firebase:firebase-config:11.4.2"
compile "com.google.firebase:firebase-crash:11.4.2"
compile "com.google.firebase:firebase-database:11.4.2"
compile "com.google.firebase:firebase-firestore:11.4.2"
compile "com.google.firebase:firebase-messaging:11.4.2"
compile "com.google.firebase:firebase-perf:11.4.2"
compile "com.google.firebase:firebase-storage:11.4.2"
2017-07-20 10:02:31 +00:00
}
```
2017-10-04 15:09:29 +00:00
Google Play services from 11.4.2 onwards require their dependencies to be downloaded from Google's Maven respository so add the
2017-09-13 15:47:22 +00:00
required reference to the repositories section of the *project* level build.gradle
`android/build.gradle`
2017-09-18 14:05:01 +00:00
```groovy
allprojects {
2017-09-13 15:47:22 +00:00
repositories {
2017-10-04 18:53:35 +00:00
// ...
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
},
// Add this below the existing maven property above:
2017-09-13 15:47:22 +00:00
maven {
url 'https://maven.google.com'
}
}
}
```
2017-05-30 15:16:56 +00:00
To install `react-native-firebase` in your project, you'll need to import the packages you need from `io.invertase.firebase` in your project's `android/app/src/main/java/com/[app name]/MainApplication.java` and list them as packages for ReactNative in the `getPackages()` function:
2017-03-02 12:50:09 +00:00
```java
package com.youcompany.application;
// ...
2017-05-30 15:16:56 +00:00
// Required package
import io.invertase.firebase.RNFirebasePackage; // < -- Add this line
// Optional packages - add as appropriate
import io.invertase.firebase.admob.RNFirebaseAdMobPackage; //Firebase AdMob
import io.invertase.firebase.analytics.RNFirebaseAnalyticsPackage; // Firebase Analytics
import io.invertase.firebase.auth.RNFirebaseAuthPackage; // Firebase Auth
import io.invertase.firebase.config.RNFirebaseRemoteConfigPackage; // Firebase Remote Config
import io.invertase.firebase.crash.RNFirebaseCrashPackage; // Firebase Crash Reporting
import io.invertase.firebase.database.RNFirebaseDatabasePackage; // Firebase Realtime Database
2017-10-03 11:03:16 +00:00
import io.invertase.firebase.firestore.RNFirebaseFirestorePackage; // Firebase Firestore
2017-05-30 15:16:56 +00:00
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage; // Firebase Cloud Messaging
2017-08-08 11:31:14 +00:00
import io.invertase.firebase.perf.RNFirebasePerformancePackage; // Firebase Performance
2017-05-30 15:16:56 +00:00
import io.invertase.firebase.storage.RNFirebaseStoragePackage; // Firebase Storage
2017-03-02 12:50:09 +00:00
// ...
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
protected List< ReactPackage > getPackages() {
return Arrays.< ReactPackage > asList(
new MainReactPackage(),
2017-05-30 15:16:56 +00:00
new RNFirebasePackage(), // < -- Add this line
// Add these packages as appropriate
new RNFirebaseAdMobPackage(),
new RNFirebaseAnalyticsPackage(),
new RNFirebaseAuthPackage(),
new RNFirebaseRemoteConfigPackage(),
new RNFirebaseCrashPackage(),
new RNFirebaseDatabasePackage(),
2017-10-03 11:03:16 +00:00
new RNFirebaseFirestorePackage(),
2017-05-30 15:16:56 +00:00
new RNFirebaseMessagingPackage(),
new RNFirebasePerformancePackage(),
new RNFirebaseStoragePackage()
2017-03-02 12:50:09 +00:00
);
}
};
// ...
}
```
2017-03-17 14:39:50 +00:00
2017-04-27 11:18:12 +00:00
## 3) Cloud Messaging (optional)
2017-04-06 12:01:18 +00:00
2017-03-17 14:39:50 +00:00
If you plan on using [Firebase Cloud Messaging ](https://firebase.google.com/docs/cloud-messaging/ ), add the following to `android/app/src/main/AndroidManifest.xml` .
Add permissions:
2017-04-11 11:50:39 +00:00
```xml
2017-03-17 14:39:50 +00:00
< manifest . . . >
< uses-permission android:name = "android.permission.INTERNET" / >
< uses-permission android:name = "android.permission.RECEIVE_BOOT_COMPLETED" / >
< uses-permission android:name = "android.permission.VIBRATE" / >
```
2017-05-12 19:34:43 +00:00
Set app [launch mode ](https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en ) inside activity props:
2017-04-11 11:50:39 +00:00
```xml
2017-05-12 19:34:43 +00:00
< activity
2017-03-17 14:39:50 +00:00
...
android:launchMode="singleTop"
>
```
Add messaging service:
2017-04-11 11:50:39 +00:00
```xml
2017-03-17 14:39:50 +00:00
< application . . . >
< service
android:name="io.invertase.firebase.messaging.MessagingService"
android:enabled="true"
android:exported="true">
< intent-filter >
< action android:name = "com.google.firebase.MESSAGING_EVENT" / >
< / intent-filter >
< / service >
< service android:name = "io.invertase.firebase.messaging.InstanceIdService" android:exported = "false" >
< intent-filter >
< action android:name = "com.google.firebase.INSTANCE_ID_EVENT" / >
< / intent-filter >
< / service >
```
2017-03-27 12:48:50 +00:00
If you would like to schedule local notifications then you also need to add the following:
2017-04-11 11:50:39 +00:00
```xml
2017-03-27 12:48:50 +00:00
< receiver android:name = "io.invertase.firebase.messaging.RNFirebaseLocalMessagingPublisher" / >
2017-05-16 05:03:07 +00:00
< receiver android:enabled = "true" android:exported = "true" android:name = "io.invertase.firebase.messaging.RNFirebaseSystemBootEventReceiver" >
2017-03-27 12:48:50 +00:00
< intent-filter >
< action android:name = "android.intent.action.BOOT_COMPLETED" / >
< action android:name = "android.intent.action.QUICKBOOT_POWERON" / >
< action android:name = "com.htc.intent.action.QUICKBOOT_POWERON" / >
< category android:name = "android.intent.category.DEFAULT" / >
< / intent-filter >
< / receiver >
```
2017-05-25 11:04:08 +00:00
2017-05-30 15:16:56 +00:00
## 4) Performance Monitoring (optional)
2017-05-25 11:04:08 +00:00
2017-08-08 11:36:30 +00:00
If you'd like to take advantage of Firebase's [Performance Monitoring ](https://firebase.google.com/docs/perf-mon/ ), the following additions
2017-05-25 11:04:08 +00:00
to your project setup are required:
In your projects `android/build.gradle` file, add the plugin to your dependencies:
2017-08-26 06:06:49 +00:00
```groovy
2017-05-25 11:04:08 +00:00
dependencies {
2017-08-26 06:06:49 +00:00
// ...
2017-05-25 11:04:08 +00:00
classpath 'com.google.firebase:firebase-plugins:1.1.0'
}
```
At the top of your `android/app/build.gradle` file, below other plugins, apply the `firebase-perf` plugin:
2017-08-26 06:06:49 +00:00
```groovy
2017-05-25 11:04:08 +00:00
apply plugin: "com.android.application"
apply plugin: "com.google.firebase.firebase-perf"
```
In the same file, add the `firebase-perf` module to your dependencies:
2017-08-26 06:06:49 +00:00
```groovy
2017-05-25 11:04:08 +00:00
dependencies {
2017-08-26 06:06:49 +00:00
// ...
2017-10-04 15:09:29 +00:00
compile "com.google.firebase:firebase-perf:11.4.2"
2017-05-25 11:04:08 +00:00
}
```