Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/module/push/push.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Controller, Post, Body } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { PushService } from './push.service';

export class PushNotificationRequest {
@IsString()
@ApiProperty({ description: '푸시 알림 제목' })
title: string;

@IsString()
@ApiProperty({ description: '푸시 알림 메시지' })
message: string;

@IsString()
@ApiProperty({ description: '푸시 알림 수신자 토큰' })
token: string;
}

@ApiTags('/push')
@Controller('push')
export class PushController {
constructor(private readonly PushService: PushService) {}
@Post('/send')
@ApiOperation({ summary: '푸시 알림 전송' })
async sendPushNotification(@Body() dto: PushNotificationRequest) {
await this.PushService.sendTestPushNotification(dto);
return;
}
}
3 changes: 2 additions & 1 deletion src/module/push/push.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { PushService } from './push.service';
import { PushRepository } from './push.repository';
import { PushController } from './push.controller';

@Module({
imports: [],
controllers: [PushController],
providers: [PushService, PushRepository],
exports: [PushService],
})
Expand Down
44 changes: 41 additions & 3 deletions src/module/push/push.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Injectable } from '@nestjs/common';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PushRepository } from './push.repository';
import * as admin from 'firebase-admin';
import { OnEvent } from '@nestjs/event-emitter';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class PushService {
export class PushService implements OnModuleInit {
private app: admin.app.App | null = null;
constructor(
private readonly pushRepository: PushRepository,
private readonly configService: ConfigService,
) {}

async onModuleInit() {
await this.initializeFirebaseAdminSdk();
}

async initializeFirebaseAdminSdk() {
const firebaseProjectId = this.configService.get<string>(
'FIREBASE_ADMIN_SDK_PROJECT_ID',
Expand Down Expand Up @@ -41,14 +45,19 @@ export class PushService {
body: string;
imageUrl?: string;
}) {
if (!this.app) {
console.error('Firebase Admin SDK is not initialized');
return;
}

const tokens = await this.pushRepository.findManyByUserId(userId);

if (tokens.length === 0) {
return;
}

try {
const response = await admin.messaging(this.app!).sendEachForMulticast({
const response = await admin.messaging(this.app).sendEachForMulticast({
tokens: tokens.map((token) => token.token),
notification: data,
});
Expand All @@ -67,4 +76,33 @@ export class PushService {
}) {
await this.pushNotification(payload);
}

// test
async sendTestPushNotification({
token,
title,
message,
}: {
token: string;
title: string;
message: string;
}) {
if (!this.app) {
console.error('Firebase Admin SDK is not initialized');
return;
}

try {
const response = await admin.messaging(this.app).sendEachForMulticast({
tokens: [token],
notification: {
title: title,
body: message,
},
});
console.log('Test push notification sent successfully:', response);
} catch (error) {
console.error('Error sending test push notification:', error);
}
}
}
Loading