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

纵饮孤独 提交于 2021-02-07 04:33:08

问题


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.

The problem I have is that FirebaseMessaging.getInstance() doesn't support the name argument.

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.

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)?


回答1:


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:

  1. Disable FirebaseInitProvider

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"
  />
  1. Call FirebaseApp.initializeApp(Context, FirebaseOptions)

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

The Google Services Plugin documentation describes how to get the values you need from the JSON:

{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

gcm_defaultSenderId: project_info/project_number

default_web_client_id: {YOUR_CLIENT}/oauth_client/client_id (client_type == 3)

ga_trackingId: {YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id

firebase_database_url: project_info/firebase_url

google_api_key: {YOUR_CLIENT}/api_key/current_key

google_crash_reporting_api_key: {YOUR_CLIENT}/api_key/current_key




回答2:


To allow a single client to receive messages from multiple senders you can use the FirebaseInstanceId.getToken(A_SENDER_ID, "FCM") method. So there is no need to have retrieve multiple Messaging instances, to manage multiple senders.

Once the message arrives you can filter on the from field of the RemoteMessage object that is passed via the onMessageReceived callback.




回答3:


If you navigate to FirebaseMessaging class, you'll see there's a package private getInstance which takes a firebaseApp. Also you'll see getInstance() is also using that with the default firebase app as the parameter. 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


来源:https://stackoverflow.com/questions/39913573/firebasemessaging-without-using-google-services-json-to-support-multiple-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!