简体   繁体   English

FirebaseMessaging 不使用 google-services.json 来支持多个项目

[英]FirebaseMessaging without using google-services.json to support multiple projects

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.我知道能够使用自定义选项创建一个命名FirebaseApp实例以用于例如FirebaseDatabase ,但我特别需要能够针对不同的(在运行时确定的)Firebase 项目使用FirebaseMessagaging

The problem I have is that FirebaseMessaging.getInstance() doesn't support the name argument.FirebaseMessaging.getInstance()的问题是FirebaseMessaging.getInstance()不支持 name 参数。

Is this in any way possible?这有可能吗?

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.我需要支持不同项目的原因是我们的客户端连接到不同的客户服务器,因此每个客户都有自己的 firebase 帐户并生成自己的通知。

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. Firebase 博客包含一篇关于这个确切问题的帖子,并提供了如何处理它的指南。

The blog post describes two steps:博客文章描述了两个步骤:

  1. Disable FirebaseInitProvider禁用 FirebaseInitProvider

Add this to the AndroidManifest.xml to disable the default initialization code:将此添加到 AndroidManifest.xml 以禁用默认初始化代码:

<provider
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:authorities="${applicationId}.firebaseinitprovider"
  tools:node="remove"
  />
  1. Call FirebaseApp.initializeApp(Context, FirebaseOptions)调用 FirebaseApp.initializeApp(Context, FirebaseOptions)

As soon as you have the necessary configuration ready, initialize Firebase on your own with:准备好必要的配置后,请使用以下命令自行初始化 Firebase:

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());

The Google Services Plugin documentation describes how to get the values you need from the JSON: Google 服务插件文档描述了如何从 JSON 获取您需要的值:

{YOUR_CLIENT} is the client object that contains a client_info/android_client_info/package_name that matches your package name (application id). {YOUR_CLIENT}是包含与您的包名称(应用程序 ID)匹配的client_info/android_client_info/package_nameclient对象。

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

To allow a single client to receive messages from multiple senders you can use the FirebaseInstanceId.getToken(A_SENDER_ID, "FCM") method.要允许单个客户端接收来自多个发件人的消息,您可以使用FirebaseInstanceId.getToken(A_SENDER_ID, "FCM")方法。 So there is no need to have retrieve multiple Messaging instances, to manage multiple senders.因此无需检索多个 Messaging 实例来管理多个发件人。

Once the message arrives you can filter on the from field of the RemoteMessage object that is passed via the onMessageReceived callback.消息到达后,您可以过滤通过onMessageReceived回调传递的RemoteMessage对象的from字段。

If you navigate to FirebaseMessaging class, you'll see there's a package private getInstance which takes a firebaseApp .如果您导航到FirebaseMessaging类,您会看到有一个包 private getInstance ,它采用firebaseApp Also you'll see getInstance() is also using that with the default firebase app as the parameter.此外,您还会看到getInstance()也将其与默认的 firebase 应用程序一起用作参数。 You can get it by reflection.你可以通过反射得到它。

val firebaseApp = buildFirebaseApp()

val method = FirebaseMessaging::class.java.getDeclaredMethod("getInstance", FirebaseApp::class.java)
// Set the non-public accessible if security allowed
method.isAccessible = true
val fcmMessaging = method.invoke(null, firebaseApp) as FirebaseMessaging

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM