使用FCM在Android应用程序中接收来自多个发件人(Firebase项目)的消息



我正试图使用本文中的指针接收来自多个Firebase项目(发送方(的消息。我已经用适当的选项初始化了FirebaseApp,并在每个发送方/项目上调用了getToken()。但这行不通。我收到的唯一FCM消息来自该项目,其google-services.json已包含在该项目中。如果我遗漏了什么,请告诉我。谢谢

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

我的工作方式与您所做的有点相似,但略有不同:我保留了从工厂方法FirebaseApp.initializeApp返回的FirebaseApp的引用,然后将变量传递给FirebaseInstanceId.getInstance()。此外,我不认为初始化应用程序FirebaseApp.initializeApp(context)的第一个调用是有用的,因为在所有情况下你都不需要它

我的代码版本是:

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

我不知道这是否有意义,但我希望它无论如何都能帮助你。

相关内容

  • 没有找到相关文章

最新更新