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();
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.
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.