handle multiple firebase config
MODIFY
src/assets/storage/schemas/configuration/configuration-save.json
src/assets/storage/schemas/configuration/configuration-save.json
"configuration"
]
}
+ },
+ "alternativeConfiguration": {
+ "type": "object",
+ "properties": {
+ "type": {
+ "type": "string"
+ },
+ "projectID": {
+ "type": "string"
+ },
+ "privateKeyID": {
+ "type": "string"
+ },
+ "privateKey": {
+ "type": "string"
+ },
+ "clientEmail": {
+ "type": "string"
+ },
+ "clientID": {
+ "type": "string"
+ },
+ "authURI": {
+ "type": "string"
+ },
+ "tokenURI": {
+ "type": "string"
+ },
+ "authProviderX509CertURL": {
+ "type": "string"
+ },
+ "clientX509CertURL": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "type",
+ "projectID",
+ "privateKeyID",
+ "privateKey",
+ "clientEmail",
+ "clientID",
+ "authURI",
+ "tokenURI",
+ "authProviderX509CertURL",
+ "clientX509CertURL"
+ ]
}
},
"required": [

MODIFY
src/notification/remote-push-notification/RemotePushNotificationTask.ts
src/notification/remote-push-notification/RemotePushNotificationTask.ts
@@ -17,6 +17,7 @@ const MODULE_NAME = 'RemotePushNotificationTask';
export default class RemotePushNotificationTask implements NotificationTask {
private firebaseConfig = Configuration.getFirebaseConfig();
private defaultApp: admin.app.App;
+ private alternativeApp: admin.app.App;
private tenantFirebaseApps: Map<string, admin.app.App> = new Map();
private initialized = false;
@@ -31,6 +32,16 @@ export default class RemotePushNotificationTask implements NotificationTask {
privateKey: this.firebaseConfig.privateKey
})
});
+ // Init alternative conf
+ if (this.firebaseConfig.alternativeConfiguration) {
+ this.alternativeApp = admin.initializeApp({
+ credential: admin.credential.cert({
+ projectId: this.firebaseConfig.alternativeConfiguration.projectID,
+ clientEmail: this.firebaseConfig.alternativeConfiguration.clientEmail,
+ privateKey: this.firebaseConfig.alternativeConfiguration.privateKey
+ })
+ }, 'alternativeApp');
+ }
// Init tenant conf
if (!Utils.isEmptyArray(this.firebaseConfig.tenants)) {
for (const tenantConfig of this.firebaseConfig.tenants) {
@@ -453,8 +464,9 @@ export default class RemotePushNotificationTask implements NotificationTask {
}
// Create message
message = this.createMessage(tenant, notificationType, title, body, data, severity);
- // Get the right firebase app
- const app = this.getFirebaseAppFromTenant(tenant);
+ // Get the right firebase apps
+ const apps = this.getFirebaseAppsFromTenant(tenant);
+ for (const app of apps) {
try {
// Send message
const response = await admin.messaging(app).sendToDevice(
@@ -505,6 +517,7 @@ export default class RemotePushNotificationTask implements NotificationTask {
detailedMessages: { error: error.stack, message }
});
}
+ }
} finally {
await Logging.traceNotificationEnd(tenant, MODULE_NAME, 'sendRemotePushNotificationToUser', startTime, notificationType, message, user.id);
}
@@ -533,11 +546,15 @@ export default class RemotePushNotificationTask implements NotificationTask {
return message;
}
- private getFirebaseAppFromTenant(tenant: Tenant): admin.app.App {
- const app = this.tenantFirebaseApps.get(tenant.id);
- if (app) {
- return app;
+ private getFirebaseAppsFromTenant(tenant: Tenant): Array<admin.app.App> {
+ const apps = [this.defaultApp];
+ const tenantApp = this.tenantFirebaseApps.get(tenant.id);
+ if (tenantApp) {
+ return [tenantApp];
+ }
+ if (this.alternativeApp) {
+ apps.push(this.alternativeApp);
}
- return this.defaultApp;
+ return apps;
}
}

MODIFY
src/types/configuration/FirebaseConfiguration.ts
src/types/configuration/FirebaseConfiguration.ts
@@ -12,4 +12,5 @@ export default interface FirebaseConfiguration {
authProviderX509CertURL: string;
clientX509CertURL: string;
tenants: TenantConfiguration<FirebaseConfiguration>[];
+ alternativeConfiguration: FirebaseConfiguration;
}

MODIFY
src/utils/Configuration.ts
src/utils/Configuration.ts
@@ -71,6 +71,9 @@ export default class Configuration {
tenantConfig.configuration.privateKey = tenantConfig.configuration.privateKey.replace(/\\n/g, '\n');
}
}
+ if (firebaseConfiguration.alternativeConfiguration) {
+ firebaseConfiguration.alternativeConfiguration.privateKey = firebaseConfiguration.alternativeConfiguration.privateKey.replace(/\\n/g, '\n');
+ }
return firebaseConfiguration;
}
}

handle multiple firebase config