简体   繁体   English

Firebase 相同应用风格的多个项目的云功能

[英]Firebase cloud functions for multiple projects at the same app flavor

In my app I'm working with multiple Firebase projects because I need my DB to be logically separated by countries.在我的应用程序中,我正在处理多个 Firebase 项目,因为我需要我的数据库在逻辑上按国家/地区分隔。

For that, I'm using FirebaseOptions to access databases from different Firebase projects at runtime.为此,我使用 FirebaseOptions 在运行时访问来自不同 Firebase 项目的数据库。

In addition, on each Firebase project I have the same cloud function, deployed respectively.此外,在每个 Firebase 项目上,我分别部署了相同的云 function。

The problem is that only the cloud function of the default Firebase project (the one declared in the google-services.json file) is working.问题是只有默认 Firebase 项目(在 google-services.json 文件中声明的项目)的云 function 正在工作。 It seems that FirebaseOptions doesn't influence the execution of cloud functions.似乎 FirebaseOptions 不影响云功能的执行。

Anyone has a clue?有人有线索吗?

  • It is important to note that I don't have different flavors per country in my app.重要的是要注意,我的应用程序中没有针对每个国家/地区的不同口味。

Solution解决方案

I could only send notification to registration tokens that were associated for a particular sender ID.我只能向与特定发件人 ID 关联的注册令牌发送通知。 Basically, I was trying to send to tokens that belonged to other project.基本上,我试图发送属于其他项目的代币。

To do this, I should have allowed multiple projects to send messages to the same client app.为此,我应该允许多个项目向同一个客户端应用程序发送消息。 To enable this feature, we need to make sure we have each project's sender ID.要启用此功能,我们需要确保我们拥有每个项目的发件人 ID。 When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field, using the token retrieval method.请求注册时,客户端应用程序多次获取令牌,每次使用令牌检索方法在受众字段中使用不同的发件人 ID。

In other terms, we'll have to call getToken() passing the sender ID and simply "FCM" (eg getToken("2xxxxx3344", "FCM")) as the parameters.换句话说,我们必须调用 getToken() 并传递发件人 ID 和简单的“FCM”(例如 getToken("2xxxxx3344", "FCM"))作为参数。 Also make sure that you call this for each sender (project) that you need.还要确保为您需要的每个发件人(项目)调用它。

Sounds like a problem in your build variants.听起来像是您的构建变体中的问题。

If im right.如果我是对的。 You can use env-variables and >firebase use (cli command) to controll your builds.您可以使用 env-variables 和 >firebase use(cli 命令)来控制您的构建。

https://firebase.google.com/docs/functions/config-env#env-variables https://firebase.google.com/docs/functions/config-env#env-variables

Initilize FireBase-Admin-SDK.初始化 FireBase-Admin-SDK。

https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app https://firebase.google.com/docs/admin/setup#add_firebase_to_your_app

A first Idea of how to import different cred.json files.关于如何导入不同的 cred.json 文件的第一个想法。

import serviceAccountProd = require("./prodCred.json");
import serviceAccountDev = require("./devCred.json");

admin.initializeApp({
  credential: admin.credential.cert(
  development ? (serviceAccountDev as admin.ServiceAccount) : (serviceAccountProd as admin.ServiceAccount)),
  databaseURL: database,
  storageBucket: bucket,
});

i didnt implemented it fully - feel free to edit我没有完全实现它 - 随意编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Cloud Functions - 如何在 multiple.then() 方法中使用相同的返回值 - Firebase Cloud Functions - How to use same return value in multiple .then() methods Express 应用程序是否始终在 Firebase Cloud Functions 中运行? - Is an express app always running in Firebase Cloud Functions? Google Cloud Functions Firebase 错误 默认的 Firebase 应用已经存在 - Google Cloud Functions Firebase Error The default Firebase app already exists 使用 Firebase 管理员 SDK,无法部署 Firebase 跨多个文件拆分的云功能:“默认 Firebase 应用程序已存在” - Using Firebase Admin SDK, Unable to Deploy Firebase Cloud Functions Split Across Multiple Files: "The default Firebase app already exists" Cloud Functions for Firebase 超时 - Cloud Functions for Firebase timeout 一个应用程序中的多个 Firebase 项目具有不同的持久性设置 - Multiple Firebase Projects in one app with different persistence setup 我们不能在 Firebase Cloud Functions 中写入多个文件吗 - Can't we Write multiple files in Firebase Cloud Functions firebase 第 2 代的云函数 - Cloud functions for firebase generation 2 Flutter/Firebase:管理功能是应用内功能还是云功能? - Flutter/Firebase: Admin features in-app or cloud functions? Firebase 从应用上下文调用的云函数始终为 null - Firebase Cloud Functions called from app context is always null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM