Austin France
Austin France

Reputation: 2501

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.

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

Upvotes: 5

Views: 3382

Answers (3)

Mahdi-Malv
Mahdi-Malv

Reputation: 19278

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

Upvotes: 1

Thorbear
Thorbear

Reputation: 2343

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

Upvotes: 4

Arthur Thompson
Arthur Thompson

Reputation: 9225

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.

Upvotes: 2

Related Questions