简体   繁体   中英

Initialize FirebaseMessage & FirebaseAnalytics with non-default FirebaseApp

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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