环境, 项目初始化, Firebase 配置的部分参照 IOS 的文档
copy google-services.json
到 ./android/app
修改 ./android/build.gradle
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 28
}
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
classpath 'com.google.gms:google-services:4.2.0'
}
}
修改 ./android/app/build.gradle
project.ext.react = [
entryFile: "index.js",
enableHermes: false, // clean and rebuild if changing
bundleInRelease: true,
]
dependencies {
implementation project(':react-native-firebase')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+" // From node_modules
//Add these lines fcm
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
// implementation 'com.google.firebase:firebase-messaging:20.0.0'
compile(project(':react-native-firebase')) {
transitive = false
}
if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
implementation jscFlavor
}
}
文件最后添加
apply plugin: 'com.google.gms.google-services'
修改包名
修改 ./android/build.gradle
applicationId "com.gz.fcmtest"
修改 MainApplication.java
的文件路径为 com.gz.fcmtest
修改 MainApplication.java
的 package 为 package com.gz.fcmtest;
修改 MainActivity.java
的 package 为 package com.gz.fcmtest;
修改 MainActivity.java
:
protected String getMainComponentName() {
return "com.gz.fcmtest";
}
修改 app.json
: "name": "com.gz.fcmtest"
修改 appDelegate.m
: moduleName:@“com.gz.fcmtest”
修改 MainApplication.java
添加
packages.add(new RNFirebaseMessagingPackage());
packages.add(new RNFirebaseNotificationsPackage());
注释
// initializeFlipper(this); // Remove this line if you don't want Flipper enabled
Android Studio 打开 android 目录, 并对项目进行构建
重构 react-native-firebase
安装 app 的时候 Android Studio
会提示错误, 根据错误进行修复
我用的 react-native-firebase
是 5.6.0
其中引入的 androidx
是旧版本的, 所以需要重构其中的引用
例如 文件 ReactNativeFirebaseAppRegistrar.java
中的 import android.support.annotation.Keep;
改成 import androidx.annotation.Keep;
android.support.annotation. --> androidx.annotation.
android.support.v4.app. --> androidx.core.app.
android.support.v4.content. --> androidx.localbroadcastmanager.content.
启动 react-native 调试服务
react-native start
curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
安装 app
react-native run-android
Android 的:
- 需要翻墙
- 需要安装 Google Service
- 不可以设置 Badge
- 到达率不是100%, 会丢失