I have more than 300+ clients in my application and I’m using Firebase Cloud Messaging API to send push notifications. Recently I have migrated to FCM V1 and used Firebase Admin SDK. We are using a single service for sending push notifications.Since we cannot initialize Firebase apps for each client when starting the service, I have initialized firebase app when sending the push notification. To avoid duplications of firebase app initialization I have used below code snippet. But still I’m getting “FirebaseApp name [project-id] already exists!” error.
private FirebaseApp getFirebaseApp(final String serverKeyJson, final String projectId) throws IOException {
for (final FirebaseApp app : FirebaseApp.getApps()) {
if (app.getOptions().getProjectId().equals(projectId)) {
return app;
}
}
final ByteArrayInputStream credentialsStream = new ByteArrayInputStream(serverKeyJson.getBytes());
final FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(credentialsStream))
.setProjectId(projectId)
.build();
return FirebaseApp.initializeApp(options, projectId);
}
Push-notification service is based on Spring-boot.
How to fix this issue?