0

In one android app, it has dependencies on some libraries and the library may have integrated with its own FCM project (should have its own pushtoken, and and its own backend to send the push against the respected pushtoken).

So each library has its own goole-services.json generated from its FCM project. and has its own token listener, e.g.

class Proj_x_FirebaseMessagingService : FirebaseMessagingService {

    public void onNewToken(String s) {
        ...
    }
}

But when run the app only one library gets the FCM pushtoken.

ref: Receive push notifications on one android app from two Firebase projects, mentioned about using getToken("2xxxxx3344", "FCM") but it is deprecated

There is also use multiple projects in your application But not really sure how to use it. Especially if it requires change inside the depended libraries.

How to work with multiple FCM projects in one app?

lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

1

Find a way to make it work with multiple FCM projects. download the google-services.json (but dont added to the app project). Also see https://github.com/firebase/firebase-android-sdk/issues/4316

Using the info from it to construct the non-default FairebaseApp (appKey needs to get from the FCM project), and from FirebaseApp it can get the pushtoken for the FCM project (if the app is just receiving the push only it actually does not need the pushtoken but only to FirebaseApp.initializeApp).

fun getSecond_FirebaseApp() {

        val fcmAppName = "send-fcmProject" 
        val devOptions = FirebaseOptions.Builder()
            .setProjectId("fcm-project-id")
            .setApplicationId("fcm-project-appid")
            .setApiKey("fcm-project-apikey")//<==id from FCM project
            //.setGcmSenderId("fcm-proj-sender-id")//<==id from FCM project
            .build()

        FirebaseApp.initializeApp(this, devOptions, fcmAppName)

        // the FirebaseApp.getInstance() will return deafult FirebaseApp which is instantiated by the FCM sdk automatically, which the app does not need to anything.

        val app = FirebaseApp.getInstance(fcmAppName)
        val firebaseMessaging = app.get(FirebaseMessaging::class.java) as FirebaseMessaging

        android.util.Log.d(
            "+++",
            "+++ FirebaseApp($fcmAppName); app.isDefaultApp: ${app.isDefaultApp}\n" +
                    "app.name: ${app.name}\n" +
                    "app.options.applicationId: ${app.options.applicationId}\n" +
                    "app.options.apiKey: ${app.options.apiKey}\n" +
                    "app.options.projectId: ${app.options.projectId}\n" +
                    "app.options.gcmSenderId: ${app.options.gcmSenderId}"
        )

        firebaseMessaging.token.addOnCompleteListener {
           
            if (it.isSuccessful) {
                // got the token
            }
        }
    }

after FirebaseApp.initializeApp(this, devOptions, fcmAppName) the app is able to receive the FCM push sent from the second FCM project in the app's single implemented FirebaseMessagingService::onMessageReceived(RemoteMessage remoteMessage).

issue: The FCM messages are received by the single FirebaseMessagingService::onMessageReceived(RemoteMessage remoteMessage), the app has to have a way to dispatch the corresponding message to the related project.

Maybe Firebase SDKs are not intended for library projects. The features available on Firebase were integrated in an application level and not on a per module or per library basis.

Does anyone know if there is documentation for using Firebase SDK in android library?

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • I have tried to setup push notifications in a library and there is just no way of doing it without depending on the app that implements it. I wanted to implement push notifications just on the library and not in the app but always get the error `"Default firebase instance for not initialized"` when trying to create a Firebase instance. I also tried to implement the `FirebaseMessagingService` in the library and then extend it in the APP and declare it in manifest. But that requires the app. To solve, I'm just gonna implement in notis in the app and pass them to the library. – therock24 Mar 30 '23 at 18:10