不使用google-services.json支持多个项目的FirebaseMessaging [英] FirebaseMessaging without using google-services.json to support multiple projects
问题描述
我知道能够使用自定义选项创建命名的 FirebaseApp
实例以与例如 FirebaseDatabase
一起使用的功能,但是我特别需要能够使用 FirebaseMessagaging
针对不同的(在运行时确定的)Firebase项目.
I am aware of the ability to create a named FirebaseApp
instance with custom options for use with, for example, FirebaseDatabase
, but I specifically need the ability to use FirebaseMessagaging
against different (determined at runtime) Firebase projects.
我遇到的问题是 FirebaseMessaging.getInstance()
不支持name参数.
The problem I have is that FirebaseMessaging.getInstance()
doesn't support the name argument.
这有可能吗?
我需要支持单独的项目的原因是我们的客户端连接到不同的客户服务器,因此每个客户都将拥有自己的firebase帐户并生成自己的通知.
The reason I need to support separate projects is that our client connects to different customers server, so each customer will have their own firebase account and be generating their own notifications.
如果不可能,是否有任何方法可以将一个客户与另一个客户隔离开,因此他们不可能将通知发送到另一位客户的设备(假设他们可以某种方式获得有效的目标设备令牌)?
If it is not possible, is there any way to isolate one customer from another, so they can't possibly send notifications to another customer's device (assuming they could obtain a valid target device token in some way)?
推荐答案
The Firebase Blog contains a post about this exact issue, and provides a guide for how to handle it.
博客文章描述了两个步骤:
The blog post describes two steps:
- 禁用FirebaseInitProvider
将其添加到AndroidManifest.xml以禁用默认初始化代码:
Add this to the AndroidManifest.xml to disable the default initialization code:
<provider
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:authorities="${applicationId}.firebaseinitprovider"
tools:node="remove"
/>
- 致电FirebaseApp.initializeApp(Context,FirebaseOptions)
准备好必要的配置后,请使用以下命令自行初始化Firebase:
As soon as you have the necessary configuration ready, initialize Firebase on your own with:
FirebaseOptions.Builder builder = new FirebaseOptions.Builder()
.setApplicationId("1:0123456789012:android:0123456789abcdef")
.setApiKey("your_api_key")
.setDatabaseUrl("https://your-app.firebaseio.com")
.setStorageBucket("your-app.appspot.com");
FirebaseApp.initializeApp(this, builder.build());
Google服务插件文档描述了如何获取值您需要使用JSON:
The Google Services Plugin documentation describes how to get the values you need from the JSON:
{YOUR_CLIENT}
是 client
对象,其中包含与您的软件包名称(应用程序ID)相匹配的 client_info/android_client_info/package_name
.
{YOUR_CLIENT}
is the client
object that contains a client_info/android_client_info/package_name
that matches your package name (application id).
google_app_id: {YOUR_CLIENT}/client_info/mobilesdk_app_id
google_app_id: {YOUR_CLIENT}/client_info/mobilesdk_app_id
gcm_defaultSenderId: project_info/project_number
gcm_defaultSenderId: project_info/project_number
default_web_client_id: {YOUR_CLIENT}/oauth_client/client_id(client_type == 3)
default_web_client_id: {YOUR_CLIENT}/oauth_client/client_id (client_type == 3)
ga_trackingId: {YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id
ga_trackingId: {YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id
firebase_database_url: project_info/firebase_url
firebase_database_url: project_info/firebase_url
google_api_key: {YOUR_CLIENT}/api_key/current_key
google_api_key: {YOUR_CLIENT}/api_key/current_key
google_crash_reporting_api_key: {YOUR_CLIENT}/api_key/current_key
google_crash_reporting_api_key: {YOUR_CLIENT}/api_key/current_key
这篇关于不使用google-services.json支持多个项目的FirebaseMessaging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!