Our business logic requires us to instantiate Firebase during runtime. We fetch the Firebase credentials (App ID, API key etc.) from a default Firebase location after knowing the user's public key and create a Firebase instance using those credentials.
This means that there are two Firebase instances used within the app:
- The default "index" Firebase that gives us credentials for the second
- The actual Firebase that we intend to use from that point forward
The second Firebase is initialised like this:
FirebaseApp app = FirebaseApp.initializeApp(<context>, <options>, <app_label>);
Our problem is that the traditional method of retrieving the FCM token using the FirebaseInstanceIdService
and onTokenRefresh()
fails since the onTokenRefresh()
method is called only by the first Firebase instance.
Directly calling String token = FirebaseInstanceId.getInstance(app).getToken();
returns null as it is never ready when called. We have even tried polling this to test if a token is generated at some point. No luck.
How can we generate an FCM token reliably from a Firebase instance instantiated during runtime?