使用 FCM V1 获取 Google 服务账户密钥

了解如何创建或使用 Google 服务账户密钥通过 FCM 发送 Android 通知。


创建新的 Google 服务账户密钥

🌐 Create a new Google Service Account Key

以下是在 EAS 中配置新的 Google 服务账户密钥以使用 FCM V1 发送 Android 通知的步骤。

🌐 Here are the steps to configure a new Google Service Account Key in EAS for sending Android Notifications using FCM V1.

1

Firebase 控制台 中为你的应用创建一个新的 Firebase 项目。如果你已经有了应用的 Firebase 项目,请继续下一步。

🌐 Create a new Firebase project for your app in the Firebase Console. If you already have a Firebase project for your app, continue to the next step.

2

在 Firebase 控制台中,打开你的项目的 项目设置 > 服务账号

🌐 In the Firebase console, open Project settings > Service accounts for your project.

3

点击 生成新的私钥,然后通过点击 生成密钥 确认。将包含私钥的 JSON 文件安全地保存好。

🌐 Click Generate New Private Key, then confirm by clicking Generate Key. Securely store the JSON file containing the private key.

4

将 JSON 文件上传到 EAS 并配置它以发送 Android 通知。这可以通过 EAS CLI 或在 EAS 仪表板 中完成。

🌐 Upload the JSON file to EAS and configure it for sending Android notifications. This can be done using EAS CLI or in EAS dashboard.

  • 运行 eas credentials
  • 选择 Android > production > Google Service Account
  • 选择 Manage your Google Service Account Key for Push Notifications (FCM V1)
  • 选择 Set up a Google Service Account Key for Push Notifications (FCM V1) > Upload a new service account key
  • 如果你之前已将 JSON 文件存储在项目目录中,EAS CLI 会自动检测该文件并提示你选择它。按 Y 继续。

注意:将该 JSON 文件添加到版本控制的忽略文件中(例如 .gitignore),以避免将其提交到你的仓库,因为它包含敏感数据。

  • 项目设置下,点击导航菜单中的凭据
  • 对于 Android,点击 添加应用标识符 或选择现有的 应用标识符
  • 服务凭据 > FCM V1 服务账号密钥 下,点击 添加服务账号密钥
  • 上传新密钥 下,上传你的 JSON 凭证并点击 保存

5

在你的项目中配置 google-services.json 文件。从 Firebase 控制台下载该文件,并将其放置在项目目录的根目录下。

🌐 Configure the google-services.json file in your project. Download it from the Firebase Console and place it at the root of your project directory.

此文件是将你的 Android 应用注册到 FCM 所必需的。你可以将此文件提交到你的代码仓库,因为它包含来自你的 Firebase 项目的公开标识符。

🌐 This file is required for your Android app to be registered with FCM. You may commit this file to your repository since it contains public-facing identifiers from your Firebase project.

注意:如果已经设置了 google-services.json,可以跳过此步骤。

app.json 中,添加 expo.android.googleServicesFile,其值为 google-services.json 的路径。

🌐 In app.json, add expo.android.googleServicesFile with its value as the path of the google-services.json.

app.json
{ "expo": { %%placeholder-start%%...%%placeholder-end%% "android": { %%placeholder-start%%...%%placeholder-end%% "googleServicesFile": "./path/to/google-services.json" } }

6

一切就绪!你现在可以使用 FCM V1 协议通过 Expo 推送通知向 Android 设备发送通知。

🌐 You're all set! You can now send notifications to Android devices via Expo Push Notifications using the FCM V1 protocol.

使用现有的 Google 服务账户密钥

🌐 Use an existing Google Service Account Key

1

在 Google Cloud 控制台中打开 IAM 管理页面。在“权限”选项卡中,找到你打算修改的 主体,然后点击铅笔图标以 编辑主体

🌐 Open the IAM Admin page in Google Cloud Console. In the Permissions tab, locate the Principal you intend to modify and click the pencil icon for Edit Principal.

2

点击 添加角色 并从下拉菜单中选择 Firebase Messaging API 管理员 角色。点击 保存

3

你必须向 EAS 指定用于发送 FCM V1 通知的 JSON 凭证文件,可以使用 EAS CLI 或在 EAS 控制台 中操作。你可以上传新的 JSON 文件或选择之前上传的文件。

🌐 You have to specify to EAS which JSON credential file to use for sending FCM V1 notifications, using EAS CLI or in EAS dashboard. You can upload a new JSON file or select a previously uploaded file.

  • 运行 eas credentials
  • 选择 Android > production > Google Service Account
  • 选择 Manage your Google Service Account Key for Push Notifications (FCM V1)
  • 选择 Set up a Google Service Account Key for Push Notifications (FCM V1) > Upload a new service account key
  • EAS CLI 会自动检测你本地计算机上的文件,并提示你选择它。按 Y 继续。

注意:将该 JSON 文件添加到版本控制的忽略文件中(例如 .gitignore),以避免将其提交到你的仓库,因为它包含敏感数据。

  • 项目设置下,点击导航菜单中的凭据
  • 对于 Android,点击 添加应用标识符 或选择现有的 应用标识符
  • 服务凭据 > FCM V1 服务账号密钥 下,点击 添加服务账号密钥
  • 上传新密钥 下,上传你的 JSON 凭证并点击 保存

4

在你的项目中配置 google-services.json 文件。从 Firebase 控制台下载该文件,并将其放置在项目目录的根目录下。

🌐 Configure the google-services.json file in your project. Download it from the Firebase Console and place it at the root of your project directory.

此文件是将你的 Android 应用注册到 FCM 所必需的。你可以将此文件提交到你的代码仓库,因为它包含来自你的 Firebase 项目的公开标识符。

🌐 This file is required for your Android app to be registered with FCM. You may commit this file to your repository since it contains public-facing identifiers from your Firebase project.

注意:如果已经设置了 google-services.json,可以跳过此步骤。

app.json 中,添加 expo.android.googleServicesFile,其值为 google-services.json 的路径。

🌐 In app.json, add expo.android.googleServicesFile with its value as the path of the google-services.json.

app.json
{ "expo": { %%placeholder-start%%...%%placeholder-end%% "android": { %%placeholder-start%%...%%placeholder-end%% "googleServicesFile": "./path/to/google-services.json" } } }

5

一切就绪!你现在可以使用 FCM V1 协议通过 Expo 推送通知向 Android 设备发送通知。

🌐 You're all set! You can now send notifications to Android devices via Expo Push Notifications using the FCM V1 protocol.