[英]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?有人有线索吗?
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.