import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import * as admin from 'firebase-admin'; @Injectable() export class FirebaseService { private readonly logger = new Logger(FirebaseService.name); constructor(private readonly configService: ConfigService) { admin.initializeApp({ credential: admin.credential.cert({ projectId: this.configService.get('FIREBASE_PROJECT_ID'), clientEmail: this.configService.get('FIREBASE_CLIENT_EMAIL'), privateKey: this.configService.get('FIREBASE_PRIVATE_KEY').replace(/\\n/g, '\n'), }), }); } sendNotification(tokens: string | string[], title: string, body: string) { this.logger.log(`Sending push notification to ${tokens}`); const message = { notification: { title, body, }, tokens: Array.isArray(tokens) ? tokens : [tokens], }; admin.messaging().sendEachForMulticast(message); } }