|
|
@@ -0,0 +1,48 @@
|
|
|
+import firebaseAdmin from 'firebase-admin';
|
|
|
+
|
|
|
+export class FCMController {
|
|
|
+ private static INSTANCE?: FCMController;
|
|
|
+
|
|
|
+ private app: firebaseAdmin.app.App;
|
|
|
+ private messaging;
|
|
|
+
|
|
|
+ private constructor() {
|
|
|
+ // Uses ENV GOOGLE_APPLICATION_CREDENTIALS to load credentials file
|
|
|
+ this.app = firebaseAdmin.initializeApp();
|
|
|
+ this.messaging = this.app.messaging();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static get instance(): FCMController {
|
|
|
+ if (!FCMController.INSTANCE) {
|
|
|
+ FCMController.INSTANCE = new FCMController();
|
|
|
+ }
|
|
|
+ return FCMController.INSTANCE;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async sendNotificationToTopic(topic: string, notification: { title: string; body: string }) {
|
|
|
+ console.log('ICON URL:', process.env.NOTIFICATION_ICON_URL);
|
|
|
+
|
|
|
+ const response = await this.messaging.send({
|
|
|
+ notification: {
|
|
|
+ imageUrl: process.env.NOTIFICATION_ICON_URL ?? '',
|
|
|
+ ...notification
|
|
|
+ },
|
|
|
+ android: {
|
|
|
+ notification: {
|
|
|
+ icon: process.env.NOTIFICATION_ICON_URL ?? '',
|
|
|
+ imageUrl: process.env.NOTIFICATION_ICON_URL ?? ''
|
|
|
+ }
|
|
|
+ },
|
|
|
+ webpush: {
|
|
|
+ headers: {
|
|
|
+ image: process.env.NOTIFICATION_ICON_URL ?? ''
|
|
|
+ },
|
|
|
+ notification: {
|
|
|
+ icon: process.env.NOTIFICATION_ICON_URL ?? ''
|
|
|
+ }
|
|
|
+ },
|
|
|
+ topic
|
|
|
+ });
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+}
|