首页指南参考教程

使用 FCM V1 添加 Google 服务账户密钥

了解如何创建或使用 Google 服务账户密钥以使用 FCM V1 发送 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 或在 Expo 仪表板 中完成。

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

  • 运行 eas credentials

    ¥Run eas credentials

  • 选择 Android > production > Google Service Account

    ¥Select Android > production > Google Service Account

  • 选择 Manage your Google Service Account Key for Push Notifications (FCM V1)

    ¥Select Manage your Google Service Account Key for Push Notifications (FCM V1)

  • 在项目设置下,单击导航菜单中的 证书

    ¥Under Project settings, click Credentials in the navigation menu

  • 对于 Android,单击添加应用标识符或选择现有应用标识符

    ¥For Android, click Add Application Identifier or select an existing Application identifier

  • 在服务凭证下,找到 FCM V1 服务账户密钥

    ¥Under Service Credentials, find FCM V1 service account key

  • 单击添加服务账户密钥

    ¥Click Add a service account key

  • 在“上传新密钥”下,上传你的 JSON 凭证,然后单击“保存”

    ¥Under Upload new key, upload your JSON credential and click Save

5

在项目中配置 google-services.json 文件。从 Firebase 控制台下载它并将其放置在项目目录的根目录中。如果你使用版本控制,请将其添加到忽略文件(例如 .gitignore),因为它包含敏感数据。

¥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. If you're using version control, add it to your ignore file (for example, .gitignore) as it contains sensitive data.

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

¥Note: You can skip this step if google-services.json has already been set up.

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

¥Then, 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 Console 中打开 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 管理员角色。单击“保存”。

¥Click Add Role and select the Firebase Messaging API Admin role from the dropdown. Click Save.

3

你必须使用 EAS CLI 或在 Expo 仪表板 中向 EAS 指定用于发送 FCM V1 通知的 JSON 凭证文件。你可以上传新的 JSON 文件或选择以前上传的文件。

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

  • 运行 eas credentials

    ¥Run eas credentials

  • 选择 Android > production > Google Service Account

    ¥Select Android > production > Google Service Account

  • 选择 Manage your Google Service Account Key for Push Notifications (FCM V1)

    ¥Select Manage your Google Service Account Key for Push Notifications (FCM V1)

  • 在项目设置下,单击导航菜单中的 证书

    ¥Under Project settings, click Credentials in the navigation menu

  • 对于 Android,单击添加应用标识符或选择现有应用标识符

    ¥For Android, click Add Application Identifier or select an existing Application identifier

  • 在服务凭证下,找到 FCM V1 服务账户密钥

    ¥Under Service Credentials, find FCM V1 service account key

  • 单击添加服务账户密钥

    ¥Click Add a service account key

  • 上传你的 JSON 凭证并单击“保存”

    ¥Upload your JSON credential and click Save

4

在项目中配置 google-services.json 文件。从 Firebase 控制台下载它并将其放置在项目目录的根目录中。如果你使用版本控制,请将其添加到忽略文件(例如 .gitignore),因为它包含敏感数据。

¥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. If you're using version control, add it to your ignore file (for example, .gitignore) as it contains sensitive data.

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

¥Note: You can skip this step if google-services.json has already been set up.

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

¥Then, 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.

Expo 中文网 - 粤ICP备13048890号