首页指南参考教程

使用 Firebase

有关入门和使用 Firebase JS SDK 和 React Native Firebase 库的指南。


Firebase 是一个后端即服务 (BaaS) 应用开发平台,提供托管后端服务,例如实时数据库、云存储、身份验证、崩溃报告、分析等。它建立在 Google 的基础设施之上,并可自动扩展。

¥Firebase is a Backend-as-a-Service (BaaS) app development platform that provides hosted backend services such as real-time database, cloud storage, authentication, crash reporting, analytics, and so on. It is built on Google's infrastructure and scales automatically.

你可以通过两种不同的方式在项目中使用 Firebase:

¥There are two different ways you can use Firebase in your projects:

  • 使用 Firebase JS SDK

    ¥Using Firebase JS SDK

  • 使用 React Native Firebase

    ¥Using React Native Firebase

React Native 同时支持 JS SDK 和原生 SDK。以下部分将指导你了解何时使用哪个 SDK 以及在 Expo 项目中使用 Firebase 所需的所有配置步骤。

¥React Native supports both the JS SDK and the native SDK. The following sections will guide you through when to use which SDK and all the configuration steps required to use Firebase in your Expo projects.

先决条件

¥Prerequisites

在继续之前,请确保你已创建一个新的 Firebase 项目或使用 Firebase 控制台

¥Before proceeding, make sure that you have created a new Firebase project or have an existing one using the Firebase console.

使用 Firebase JS SDK

¥Using Firebase JS SDK

Firebase JS SDK 是一个 JavaScript 库,可让你与项目中的 Firebase 服务进行交互。它支持 React Native 应用中的 验证Firestore实时数据库贮存 等服务。

¥The Firebase JS SDK is a JavaScript library that allows you to interact with Firebase services in your project. It supports services such as Authentication, Firestore, Realtime Database, and Storage in a React Native app.

何时使用 Firebase JS SDK

¥When to use Firebase JS SDK

当你满足以下条件时,可以考虑使用 Firebase JS SDK:

¥You can consider using the Firebase JS SDK when you:

  • 想要在你的应用中使用 Firebase 服务,例如身份验证、Firestore、实时数据库和存储,并且想要使用 Expo 开发你的应用。

    ¥Want to use Firebase services such as Authentication, Firestore, Realtime Database, and Storage in your app and want to develop your app with Expo Go.

  • 想要快速开始使用 Firebase 服务。

    ¥Want a quick start with Firebase services.

  • 想要创建适用于 Android、iOS 和 Web 的通用应用。

    ¥Want to create a universal app for Android, iOS, and the web.

注意事项

¥Caveats

Firebase JS SDK 不支持移动应用的所有服务。其中一些服务包括 Analytics、Dynamic Links 和 Crashlytics。如果你想使用这些服务,请参阅 React Native Firebase 部分。

¥Firebase JS SDK does not support all services for mobile apps. Some of these services are Analytics, Dynamic Links and Crashlytics. See the React Native Firebase section if you want to use these services.

安装并初始化 Firebase JS SDK

¥Install and initialize Firebase JS SDK

以下小节使用 firebase@9.x.x。Expo SDK 不会强制或推荐在你的应用中使用任何特定版本的 Firebase。

¥The following sub-sections use firebase@9.x.x. Expo SDK does not enforce or recommend any specific version of Firebase to use in your app.

如果你在项目中使用旧版本的 firebase 库,则可能需要在 Firebase JS SDK 文档 的帮助下调整代码示例以匹配你正在使用的版本。

¥If you are using an older version of the firebase library in your project, you may have to adapt the code examples to match the version that you are using with the help of the Firebase JS SDK documentation.

1

安装 SDK

¥Install the SDK

创建 Expo 项目 后,你可以使用以下命令安装 Firebase JS SDK:

¥After you have created your Expo project, you can install the Firebase JS SDK using the following command:

Terminal
npx expo install firebase

2

在你的项目中初始化 SDK

¥Initialize the SDK in your project

要初始化 Expo 项目中的 Firebase 实例,你必须创建一个配置对象并将其传递给从 firebase/app 模块导入的 initializeApp() 方法。

¥To initialize the Firebase instance in your Expo project, you must create a config object and pass it to the initializeApp() method imported from the firebase/app module.

配置对象需要 API 密钥和其他唯一标识符。要获取这些值,你必须在 Firebase 项目中注册一个 Web 应用。你可以在 Firebase 文档 文件中找到这些说明。

¥The config object requires an API key and other unique identifiers. To obtain these values, you will have to register a web app in your Firebase project. You can find these instructions in the Firebase documentation.

获得 API 密钥和其他标识符后,你可以通过在项目的根目录或保存配置文件的任何其他目录中创建新的 firebaseConfig.js 文件来粘贴以下代码片段。

¥After you have the API key and other identifiers, you can paste the following code snippet by creating a new firebaseConfig.js file in your project's root directory or any other directory where you keep the configuration files.

firebaseConfig.js
import { initializeApp } from 'firebase/app';

// Optionally import the services that you want to use
// import {...} from "firebase/auth";
// import {...} from "firebase/database";
// import {...} from "firebase/firestore";
// import {...} from "firebase/functions";
// import {...} from "firebase/storage";

// Initialize Firebase
const firebaseConfig = {
  apiKey: 'api-key',
  authDomain: 'project-id.firebaseapp.com',
  databaseURL: 'https://project-id.firebaseio.com',
  projectId: 'project-id',
  storageBucket: 'project-id.appspot.com',
  messagingSenderId: 'sender-id',
  appId: 'app-id',
  measurementId: 'G-measurement-id',
};

const app = initializeApp(firebaseConfig);
// For more information on how to access Firebase in your project,
// see the Firebase documentation: https://firebase.google.com/docs/web/setup#access-firebase

你无需安装其他插件或配置即可使用 Firebase JS SDK。

¥You do not have to install other plugins or configurations to use Firebase JS SDK.

Firebase 版本 9 及更高版本提供模块化 API。你可以直接从 firebase 包中导入你想要使用的任何服务。例如,如果你想在项目中使用身份验证服务,你可以从 firebase/auth 包中导入 auth 模块。

¥Firebase version 9 and above provide a modular API. You can directly import any service you want to use from the firebase package. For example, if you want to use an authentication service in your project, you can import the auth module from the firebase/auth package.

故障排除提示:如果你遇到与 Firebase JS SDK 身份验证持久性相关的问题,请参阅 快照测试 指南。

3

配置 Metro

¥Configure Metro

如果你使用的是 Firebase 版本 9.7.x 及更高版本,则需要将以下配置添加到 Metro.config.js 文件中,以确保正确打包 Firebase JS SDK。

Expo CLI 使用 Metro 打包你的 JavaScript 代码和资源,并添加 支持更多文件扩展名

¥Expo CLI uses Metro to bundle your JavaScript code and assets, and add support for more file extensions.

首先使用以下命令在项目的根目录中生成模板文件 Metro.config.js:

¥Start by generating the template file metro.config.js in your project's root directory using the following command:

Terminal
npx expo customize metro.config.js

然后,使用以下配置更新文件:

¥Then, update the file with the following configuration:

metro.config.js
const { getDefaultConfig } = require('@expo/metro-config');

const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push('cjs');

module.exports = defaultConfig;

下一步

¥Next steps

验证

有关如何在项目中使用身份验证的更多信息,请参阅 Firebase 文档。

Firestore

有关如何在项目中使用 Firestore 数据库的更多信息,请参阅 Firebase 文档。

实时数据库

有关如何在项目中使用实时数据库的更多信息,请参阅 Firebase 文档。

贮存

有关如何使用存储的更多信息,请参阅 Firebase 文档。

Firebase 存储示例

通过我们的示例了解如何在 Expo 项目中使用 Firebase Storage。

管理 Firebase 项目的 API 密钥

有关在 Firebase 项目中管理 API 密钥和唯一标识符的详细信息。

从 Expo Firebase 包迁移到 React Native Firebase

有关从 expo-firebase-analytics 或 expo-firebase-recaptcha 包迁移到 React Native Firebase 的更多信息。

使用 React Native Firebase

¥Using React Native Firebase

React Native Firebase 通过将 Android 和 iOS 的原生 SDK 封装到 JavaScript API 中来提供对原生代码的访问。每个 Firebase 服务都作为模块提供,可以作为依赖添加到你的项目中。例如,auth 模块提供对 Firebase 身份验证服务的访问。

¥React Native Firebase provides access to native code by wrapping the native SDKs for Android and iOS into a JavaScript API. Each Firebase service is available as a module that can be added as a dependency to your project. For example, the auth module provides access to the Firebase Authentication service.

何时使用 React Native Firebase

¥When to use React Native Firebase

在以下情况下,你可以考虑使用 React Native Firebase:

¥You can consider using React Native Firebase when:

  • 你的应用需要访问 Firebase JS SDK 不支持的 Firebase 服务,例如 动态链接崩溃解决方案 等。有关原生 SDK 提供的附加功能的更多信息,请参阅 React Native Firebase 文档

    ¥Your app requires access to Firebase services not supported by the Firebase JS SDK, such as Dynamic Links, Crashlytics, and so on. For more information on the additional capabilities offered by the native SDK's, see React Native Firebase documentation.

  • 你想在应用中使用原生 SDK。

    ¥You want to use native SDKs in your app.

  • 你有一个裸露的 React Native 应用,已配置了 React Native Firebase,但正在迁移以使用 Expo SDK。

    ¥You have a bare React Native app with React Native Firebase already configured but are migrating to use Expo SDK.

  • 你想在你的应用中使用 Firebase 分析

    ¥You want to use Firebase Analytics in your app.

Migrating from Expo Firebase packages?

如果你的项目之前使用过 expo-firebase-analyticsexpo-firebase-recaptcha 包,你可以迁移到 React Native Firebase 库。欲了解更多信息,请参阅 Firebase 迁移指南

¥If your project has been previously using expo-firebase-analytics and expo-firebase-recaptcha packages, you can migrate to the React Native Firebase library. For more information, see Firebase migration guide.

注意事项

¥Caveats

React Native Firebase 需要 自定义原生代码,不能与 Expo Go 一起使用

¥React Native Firebase requires custom native code and cannot be used with Expo Go.

安装并初始化 React Native Firebase

¥Install and initialize React Native Firebase

1

安装 expo-dev-client

¥Install expo-dev-client

由于 React Native Firebase 需要自定义原生代码,因此你需要在项目中安装 expo-dev-client 库。它还允许使用 配置插件 配置 React Native Firebase 所需的任何原生代码,而无需自己编写原生代码。

¥Since React Native Firebase requires custom native code, you need to install the expo-dev-client library in your project. It also allows configuring any native code required by React Native Firebase using Config plugins without writing native code yourself.

要安装 expo-dev-client,请在项目中运行以下命令:

¥To install expo-dev-client, run the following command in your project:

Terminal
npx expo install expo-dev-client

2

安装 React Native Firebase

¥Install React Native Firebase

要使用 React Native Firebase,需要安装 @react-native-firebase/app 模块。该模块为所有其他模块提供核心功能。它还使用配置插件在你的项目中添加自定义原生代码。你可以使用以下命令安装它:

¥To use React Native Firebase, it is necessary to install the @react-native-firebase/app module. This module provides the core functionality for all other modules. It also adds custom native code in your project using a config plugin. You can install it using the following command:

Terminal
npx expo install @react-native-firebase/app

此时,你必须遵循 React Native Firebase 文档 中的说明,因为它涵盖了使用库配置项目所需的所有步骤。

¥At this point, you must follow the instructions from React Native Firebase documentation as it covers all the steps required to configure your project with the library.

在项目中配置了 React Native Firebase 库后,请返回本指南以了解如何在下一步中运行项目。

¥Once you have configured the React Native Firebase library in your project, come back to this guide to learn how to run your project in the next step.

3

运行项目

¥Run the project

如果你使用的是 EAS 构建,你可以在你的设备上创建并安装开发版本。在创建开发版本之前,你不需要在本地运行项目。有关创建开发版本的更多信息,请参阅有关 安装开发版本 的部分。

¥If you are using EAS Build, you can create and install a development build on your devices. You do not need to run the project locally before creating a development build. For more information on creating a development build, see the section on installing a development build.

Run project locally?

如果你想在本地运行项目,则需要在你的计算机上安装并配置 Android Studio 和 Xcode。有关详细信息,请参阅 本地应用开发 指南。

¥If you want to run the project locally, you need both Android Studio and Xcode installed and configured on your machine. See Local app development guide for more information.

如果特定的 React Native Firebase 模块需要自定义原生配置步骤,你必须将其添加为 plugin应用配置 文件。然后,要在本地运行项目,请在运行 npx expo run 命令之前运行 npx expo prebuild --clean 命令以应用原生更改。

¥If a particular React Native Firebase module requires custom native configuration steps, you must add it as a plugin to app config file. Then, to run the project locally, run the npx expo prebuild --clean command to apply the native changes before the npx expo run commands.

下一步

¥Next steps

配置 React Native Firebase 库后,你可以在 Expo 项目中使用它提供的任何模块。

¥After configuring React Native Firebase library, you can use any module it provides in your Expo project.

React Native Firebase 文档

有关安装和使用 React Native Firebase 特定模块的更多信息,我们建议你查看他们的文档。

Expo 中文网 - 粤ICP备13048890号