This commit is contained in:
parent
38161e67ae
commit
880062e136
|
@ -2,15 +2,12 @@ import express, { query } from 'express';
|
||||||
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일
|
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일
|
||||||
import { dbConfig } from '../private/config.js';
|
import { dbConfig } from '../private/config.js';
|
||||||
|
|
||||||
// import { HandlePush } from './push.js';
|
import { HandlePush } from './push.js';
|
||||||
// const cmcd = "/JJ";
|
|
||||||
// const db = "/JJ/db";
|
|
||||||
// const app = express();
|
|
||||||
// app.use(express.json());
|
|
||||||
|
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.post('/push', HandlePush);
|
||||||
|
|
||||||
router.get('/version', async (req, res) => {
|
router.get('/version', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const connection = await createDatabaseConnection(dbConfig);
|
const connection = await createDatabaseConnection(dbConfig);
|
||||||
|
|
76
apps/push.js
76
apps/push.js
|
@ -1,45 +1,51 @@
|
||||||
import { admin } from '../firebaseConfig.js';
|
import apn from 'apn';
|
||||||
|
import { bundleID, apnKey, apnKeyID, apnTeamID } from '../private/config.js';
|
||||||
|
|
||||||
|
// APNs 옵션 설정
|
||||||
|
const options = {
|
||||||
|
token: {
|
||||||
|
key: `../private/${apnKey}`, // 다운로드한 .p8 파일 경로
|
||||||
|
keyId: apnKeyID, // Apple Developer Console에서 얻은 키 ID
|
||||||
|
teamId: apnTeamID, // Apple Developer Console에서 얻은 팀 ID
|
||||||
|
},
|
||||||
|
production: false, // 프로덕션(실제 배포 환경)인 경우 true로 설정
|
||||||
|
};
|
||||||
|
|
||||||
|
const apnProvider = new apn.Provider(options);
|
||||||
|
|
||||||
export function HandlePush(req, res) {
|
export function HandlePush(req, res) {
|
||||||
const { fcmToken, title, body, parameter, sound, badge } = req.body;
|
const { deviceToken, title, body, parameter, sound, badge } = req.body;
|
||||||
|
|
||||||
if (!fcmToken || !title || !body) {
|
if (!deviceToken || !title || !body) {
|
||||||
res.status(400).json({ error: 'fcmToken, title, body, and parameter are required' });
|
res.status(400).json({ error: 'deviceToken, title, and body are required' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = {
|
// APNs Notification 설정
|
||||||
token: fcmToken,
|
const notification = new apn.Notification();
|
||||||
notification: {
|
notification.topic = bundleID; // 애플리케이션 번들 ID
|
||||||
title: title,
|
notification.expiry = Math.floor(Date.now() / 1000) + 3600; // 1시간 후 만료
|
||||||
body: body,
|
notification.badge = badge ? Number(badge) : 0;
|
||||||
},
|
notification.sound = sound || "default";
|
||||||
apns: {
|
notification.alert = {
|
||||||
payload: {
|
title: title,
|
||||||
aps: {
|
body: body,
|
||||||
alert: {
|
|
||||||
title: title,
|
|
||||||
body: body,
|
|
||||||
parameter: parameter,
|
|
||||||
},
|
|
||||||
sound: sound || 'default',
|
|
||||||
badge: badge ? Number(badge) : 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
notification.payload = { parameter: parameter };
|
||||||
|
|
||||||
|
// APNs를 통해 푸시 알림 전송
|
||||||
// const jjApp = admin.app('jjungtable');
|
apnProvider.send(notification, deviceToken).then((response) => {
|
||||||
|
if (response.sent.length > 0) {
|
||||||
admin.app('jjungtable').messaging().send(message)
|
console.log('Successfully sent message:', response.sent);
|
||||||
.then((response) => {
|
|
||||||
console.log('Successfully sent message:', response);
|
|
||||||
res.status(200).json({ message: 'Successfully sent message' });
|
res.status(200).json({ message: 'Successfully sent message' });
|
||||||
})
|
} else {
|
||||||
.catch((error) => {
|
console.error('Failed to send message:', response.failed);
|
||||||
console.error('Error sending message:', error);
|
res.status(500).json({ error: 'Failed to send message', details: response.failed });
|
||||||
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
}
|
||||||
});
|
}).catch((error) => {
|
||||||
|
console.error('Error sending message:', error);
|
||||||
|
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
||||||
|
}).finally(() => {
|
||||||
|
apnProvider.shutdown(); // APNs 연결 종료
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user