You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程方式在Firebase创建项目并配置安卓/iOS应用?

实现Firebase项目注册、应用添加及密钥获取的自动化流程

完全可以通过Firebase CLI或者Google Cloud/Firebase官方API实现全流程自动化,以下是具体实现方案:

使用Firebase CLI实现自动化

前置准备

  1. 安装Firebase CLI:npm install -g firebase-tools
  2. 完成非交互式身份授权(适合自动化脚本):firebase login:ci

具体步骤

  • 注册Firebase项目
    运行命令创建项目,需确保项目ID全局唯一:

    firebase projects:create --display-name "自动化测试项目" auto-test-project-2024
    
  • 添加Android应用
    指定包名和应用名称,关联到目标项目:

    firebase apps:create android --package-name com.example.autotest.android --display-name "自动化Android应用" auto-test-project-2024
    
  • 添加iOS应用
    指定Bundle ID和应用名称:

    firebase apps:create ios --bundle-id com.example.autotest.ios --display-name "自动化iOS应用" auto-test-project-2024
    
  • 获取Sender ID和Server Key

    • Sender ID:通过项目配置命令获取,对应messagingSenderId字段:
      firebase projects:config:get --project auto-test-project-2024 | grep messagingSenderId
      
    • Server Key(Cloud Messaging API密钥):借助gcloud命令筛选获取:
      gcloud services api-keys list --filter="displayName:Firebase Cloud Messaging API key" --project auto-test-project-2024 --format="value(metadata.apiKey)"
      

使用Firebase Management API实现自动化

前置准备

  1. 创建Google Cloud服务账号,授予Firebase Admin SDK Administrator Service Agent和API Keys Admin权限
  2. 下载服务账号密钥JSON文件,设置环境变量GOOGLE_APPLICATION_CREDENTIALS指向该文件

具体步骤

  • 创建Firebase项目
    调用Firebase Management API的projects.create接口,传入项目名称、显示名称等参数。

  • 添加Android/iOS应用

    • Android:调用projects.androidApps.create接口,传入项目ID、包名、应用名称
    • iOS:调用projects.iosApps.create接口,传入项目ID、Bundle ID、应用名称
  • 获取Sender ID和Server Key

    • Sender ID:调用projects.get接口,返回结果中的projectNumber即为Sender ID
    • Server Key:调用Google Cloud API Keys的apiKeys.list接口,筛选displayName包含Firebase Cloud Messaging的密钥,提取apiKey字段

注意事项

  • 所有API调用需使用OAuth2.0身份验证,服务账号需拥有足够权限
  • 项目ID、应用包名/Bundle ID需保证全局唯一,重复创建会报错

内容的提问来源于stack exchange,提问作者Магомед Якубов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 05:40:37