This commit is contained in:
parent
880062e136
commit
b43643ee16
22
apps/push.js
22
apps/push.js
|
@ -14,26 +14,22 @@ const options = {
|
||||||
const apnProvider = new apn.Provider(options);
|
const apnProvider = new apn.Provider(options);
|
||||||
|
|
||||||
export function HandlePush(req, res) {
|
export function HandlePush(req, res) {
|
||||||
const { deviceToken, title, body, parameter, sound, badge } = req.body;
|
// 쿼리 파라미터에서 값 추출
|
||||||
|
const { id: deviceToken, title, body, parameter, sound = "default", badge = 0 } = req.query;
|
||||||
|
|
||||||
if (!deviceToken || !title || !body) {
|
if (!deviceToken || !title || !body) {
|
||||||
res.status(400).json({ error: 'deviceToken, title, and body are required' });
|
res.status(400).json({ error: 'deviceToken, title, and body are required' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// APNs Notification 설정
|
|
||||||
const notification = new apn.Notification();
|
const notification = new apn.Notification();
|
||||||
notification.topic = bundleID; // 애플리케이션 번들 ID
|
notification.topic = "your.bundle.id";
|
||||||
notification.expiry = Math.floor(Date.now() / 1000) + 3600; // 1시간 후 만료
|
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
|
||||||
notification.badge = badge ? Number(badge) : 0;
|
notification.badge = Number(badge);
|
||||||
notification.sound = sound || "default";
|
notification.sound = sound;
|
||||||
notification.alert = {
|
notification.alert = { title, body };
|
||||||
title: title,
|
notification.payload = { parameter };
|
||||||
body: body,
|
|
||||||
};
|
|
||||||
notification.payload = { parameter: parameter };
|
|
||||||
|
|
||||||
// APNs를 통해 푸시 알림 전송
|
|
||||||
apnProvider.send(notification, deviceToken).then((response) => {
|
apnProvider.send(notification, deviceToken).then((response) => {
|
||||||
if (response.sent.length > 0) {
|
if (response.sent.length > 0) {
|
||||||
console.log('Successfully sent message:', response.sent);
|
console.log('Successfully sent message:', response.sent);
|
||||||
|
@ -46,6 +42,6 @@ export function HandlePush(req, res) {
|
||||||
console.error('Error sending message:', error);
|
console.error('Error sending message:', error);
|
||||||
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
apnProvider.shutdown(); // APNs 연결 종료
|
apnProvider.shutdown();
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user