-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { FirebaseService } from './firebase.service'; | ||
|
||
@Module({ | ||
providers: [FirebaseService], | ||
exports: [FirebaseService], | ||
}) | ||
export class FirebaseModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as admin from 'firebase-admin'; | ||
import { TokenMessage } from 'firebase-admin/lib/messaging/messaging-api'; | ||
import { DateTime } from 'luxon'; | ||
import * as serverAccount from 'secret/firebase.json'; | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { FCMTokenMessageModel } from './models/token-data.model'; | ||
|
||
@Injectable() | ||
export class FirebaseService { | ||
constructor() { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serverAccount as admin.ServiceAccount), | ||
}); | ||
} | ||
|
||
async tokenMessageGenerator({ | ||
token, | ||
title, | ||
body, | ||
data, | ||
}: FCMTokenMessageModel): Promise<TokenMessage> { | ||
const message: TokenMessage = { | ||
notification: { | ||
title, | ||
body, | ||
}, | ||
token, | ||
data: data as unknown as { [key: string]: string }, | ||
android: { | ||
priority: 'high', | ||
ttl: 60 * 1000 * 30, // 30 minutes | ||
notification: { | ||
priority: 'high', | ||
sound: 'default', | ||
channelId: 'high_importance_channel', | ||
}, | ||
}, | ||
apns: { | ||
headers: { | ||
'apns-priority': '10', | ||
'apns-expiration': (60 * 30).toString(), // 30 minutes | ||
}, | ||
payload: { | ||
aps: { | ||
badge: 0, | ||
sound: 'default', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return message; | ||
} | ||
|
||
async sendNotificationByToken(notificationData: FCMTokenMessageModel): Promise<boolean> { | ||
try { | ||
await admin | ||
.messaging() | ||
.send(await this.tokenMessageGenerator(notificationData)) | ||
.then((response) => { | ||
console.log( | ||
DateTime.now().toISO({ includeOffset: true }), | ||
notificationData.token, | ||
notificationData.body, | ||
response, | ||
); | ||
}); | ||
|
||
return true; | ||
} catch (error) { | ||
console.error('Error sending message:', error); | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './firebase.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface FCMTokenMessageModel { | ||
token: string; | ||
title: string; | ||
body: string; | ||
data?: MessageDataModel; | ||
} | ||
|
||
export interface MessageDataModel { | ||
clickAction: string; | ||
data?: string; // JSON string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './prisma'; | ||
export * from './coolsms'; | ||
export * from './s3'; | ||
export * from './firebase'; |