[英]Receive messages from multiple senders (Firebase projects) in Android app using FCM
I'm trying to receive messages from multiple Firebase projects(senders) using the pointers in this post .我正在尝试使用这篇文章中的指针从多个 Firebase 项目(发件人)接收消息。 I have initialized the FirebaseApp with appropriate options and have called
getToken()
on each of the senders/projects.我已经使用适当的选项初始化了 FirebaseApp,并在每个发件人/项目上调用了
getToken()
。 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.
我收到的唯一 FCM 消息来自其 google-services.json 已包含在项目中的项目。 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()
.对我有用的方式与您所做的有点相似,但略有不同:我保留了从工厂方法
FirebaseApp.initializeApp
返回的FirebaseApp
引用,然后将变量传递给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.另外,我不认为第一次调用 initialize app
FirebaseApp.initializeApp(context)
是有用的,因为在所有情况下你都不需要它。
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.我不知道这是否有意义,但我希望它无论如何都能帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.