mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-14 09:37:20 +00:00
30 lines
975 B
TypeScript
30 lines
975 B
TypeScript
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);
|
|
}
|
|
}
|