1

I initialize my FirebaseApp using

val prodFirebaseOptions: FirebaseOptions = FirebaseOptions.Builder()
    .setApplicationId(context.getString(R.string.prod_google_app_id)) // Required for Analytics.
    .setApiKey(context.getString(R.string.prod_google_api_key)) // Required for Auth.
    .setDatabaseUrl(context.getString(R.string.prod_firebase_database_url)) // Required for RTDB.
    .setGcmSenderId(context.getString(R.string.prod_gcm_defaultSenderId))
    .setProjectId(context.getString(R.string.prod_project_id))
    .setStorageBucket(context.getString(R.string.prod_storage_bucket))
    .build()
FirebaseApp.initializeApp(context, prodFirebaseOptions, "production")
val firebaseApp = FirebaseApp.getInstance("production")

And then to initialize all the services I use:

val firestore = FirebaseFirestore.getInstance(firebaseApp)
val auth = FirebaseAuth.getInstance(firebaseApp)
val storage = FirebaseStorage.getInstance(firebaseApp)

When I want to initialize my FirebaseMessaging and FirebaseAnalytics, there is not getInstance method that accepts a FirebaseApp:

val messaging = FirebaseMessaging.getInstance() // will use "default" app instead of providing 
val analytics = FirebaseAnalytics.getInstance() // will use "default" app instead of providing 

In addition to that, there is no way to initialize the FirebaseMessagingService with the non-default app.

Is there a solution to this problem? Can I somewhat change the default app maybe?


Versions:

  • com.google.firebase:firebase-messaging:20.1.0
  • com.google.firebase:firebase-analytics:17.2.1
  • com.google.firebase:firebase-core:17.2.1
Danpe
  • 18,668
  • 21
  • 96
  • 131
  • In case you want to receive messages from a project other than the one defined in your app's `google-services.json` check out this answer that doesn't need a separate instance of `FirebaseMessaging`/`FirebaseApp` https://stackoverflow.com/a/62710006/1916449 – arekolek Jul 03 '20 at 07:16

1 Answers1

0

It's not possible. These products depend very heavily on the fact that there can be only one instance per app.

This is especially true for Cloud Messaging, where incoming messages are actually handled by Play services (which runs in another privileged process) and has to assume that there is only one installed Firebase app instance (the default) that can handle the message.

The issue with Analytics is that only one app can ever log user behavior. That behavior can happen automatically, with any APIs. It doesn't make sense for two apps to automatically log the same behavior, so it's only allowed for the default app to handle that data.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • You say it's not possible even in principle, but looks like someone made it to work through reflection https://stackoverflow.com/a/62256137/1916449 – arekolek Jul 03 '20 at 08:20