1

I'm trying to receive messages from multiple Firebase projects(senders) using the pointers in this post. I have initialized the FirebaseApp with appropriate options and have called getToken() on each of the senders/projects. But this doesn't work. The only FCM messages that I receive are from the project whose google-services.json has been included in the project. Please let me know if I am missing something here. Thanks.

            FirebaseApp.initializeApp(context);

            FirebaseOptions options1 = new FirebaseOptions.Builder()
                    .setApiKey("apiKey1")
                    .setApplicationId("appId1")
                    .setGcmSenderId("senderId1")
                    .setStorageBucket("bucket1")
                    .setDatabaseUrl("database1")
                    .build();
            FirebaseApp.initializeApp(context, options1, "project1");

            FirebaseOptions options2 = new FirebaseOptions.Builder()
                    .setApiKey("apikey2")
                    .setApplicationId("appId2")
                    .setGcmSenderId("sender2")
                    .setStorageBucket("bucket2")
                    .setDatabaseUrl("database2")
                    .build();
            FirebaseApp.initializeApp(context, options2, "project2");

            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {

                    try {
                        String token1 = FirebaseInstanceId.getInstance(FirebaseApp.getInstance("project1")).getToken("sender1", FirebaseMessaging.INSTANCE_ID_SCOPE);
                        String token2 = FirebaseInstanceId.getInstance(FirebaseApp.getInstance("project2")).getToken("sender2", FirebaseMessaging.INSTANCE_ID_SCOPE);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {

                }
            }.execute();

           FirebaseMessaging fcm = FirebaseMessaging.getInstance();                
viv3k
  • 621
  • 4
  • 10
  • Did you found any solution. Please update. I am also running into similar problem. – Abhinav Suthar May 09 '20 at 18:03
  • I have a android library which uses FCM to receive commands. Problem occurs when I import my library in another app which is also using FCM. So my FCM service(library) did not receive any FCM notifications, but the app's FCM service receives all the notifications. So my question is, How to connect a FCM service to a particular firebase project? Help Me! – Abhinav Suthar May 09 '20 at 18:06
  • @AbhinavSuthar We have worked around the problem using build variants. We had two separate build variants with different package names and collected tokens in separate workflows. This was a cleaner way to solve my problem, but yes, I haven't found a solution to the original problem statement. – viv3k May 11 '20 at 07:31

1 Answers1

0

The way that worked for me is a little bit similar to what you did, but with a slight difference: I was keeping reference of the FirebaseApp returned from the Factory Method FirebaseApp.initializeApp and then passing the variable to the FirebaseInstanceId.getInstance(). Also, I don't think the first call for initialize app FirebaseApp.initializeApp(context) is useful since in all cases you will not need it.

My version of your code would be:

            //FirebaseApp.initializeApp(context);

            FirebaseOptions options1 = new FirebaseOptions.Builder()
                    .setApiKey("apiKey1")
                    .setApplicationId("appId1")
                    .setGcmSenderId("senderId1")
                    .setStorageBucket("bucket1")
                    .setDatabaseUrl("database1")
                    .build();
            FirebaseApp app1 = FirebaseApp.initializeApp(context, options1, "project1");

            FirebaseOptions options2 = new FirebaseOptions.Builder()
                    .setApiKey("apikey2")
                    .setApplicationId("appId2")
                    .setGcmSenderId("sender2")
                    .setStorageBucket("bucket2")
                    .setDatabaseUrl("database2")
                    .build();
            FirebaseApp app2 = FirebaseApp.initializeApp(context, options2, "project2");

            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {

                    try {
                        String token1 = FirebaseInstanceId.getInstance(app1).getToken("sender1", FirebaseMessaging.INSTANCE_ID_SCOPE);
                        String token2 = FirebaseInstanceId.getInstance(app2).getToken("sender2", FirebaseMessaging.INSTANCE_ID_SCOPE);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {

                }
            }.execute();

           FirebaseMessaging fcm = FirebaseMessaging.getInstance();   

I don't know if it makes sense, but I hope that it will be able to help you anyways.

MarounG
  • 164
  • 3
  • 8
  • you got the token, how does it receive the FCM message from different FCM project? will the FirebaseMessagingService.onMessageReceived(message: RemoteMessage) get called for each FCM project separately? – lannyf Mar 16 '23 at 18:42
  • As far as I remember (since it's been a long time that I didn't touch the project), no need to implment `FirebaseMessagingService.onMessageReceived(message: RemoteMessage)` twice. If we implement it once, the call might be received for both apps through the same implementation. – MarounG Apr 03 '23 at 12:09