Skip to content

Commit ff7eb44

Browse files
committed
feat: 백그라운드 혹은 앱이 종료되었을 때의 푸시 배지 구현
1 parent 298d2fe commit ff7eb44

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

Firebase/functions/src/fcm/notification.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,19 @@ export const sendPushNotification = onTaskDispatched({
9797
};
9898
await notificationDocRef.set(notificationData, { merge: true });
9999

100-
// 1. 사용자 FCM 토큰 가져오기
101-
const tokenDoc = await admin.firestore().doc(`users/${userId}/userData/tokens`).get();
100+
// 1. 사용자 FCM 토큰과 읽지 않은 알림 수 가져오기
101+
const unreadCountPromise = admin.firestore()
102+
.collection(`users/${userId}/notifications`)
103+
.where("isRead", "==", false)
104+
.count()
105+
.get();
106+
const tokenDocPromise = admin.firestore().doc(`users/${userId}/userData/tokens`).get();
107+
const [tokenDoc, unreadCountSnapshot] = await Promise.all([
108+
tokenDocPromise,
109+
unreadCountPromise
110+
]);
102111
const fcmToken = tokenDoc.data()?.fcmToken;
112+
const unreadNotificationCount = unreadCountSnapshot.data().count;
103113

104114
if (!fcmToken) {
105115
logger.warn(`사용자 ${userId}의 fcmToken이 없어 푸시 발송은 건너뜁니다. Firestore에는 기록했습니다.`);
@@ -113,7 +123,14 @@ export const sendPushNotification = onTaskDispatched({
113123
todoId: todoId,
114124
todoKind: todoKind
115125
},
116-
apns: { payload: { aps: { sound: "default" } } },
126+
apns: {
127+
payload: {
128+
aps: {
129+
sound: "default",
130+
badge: unreadNotificationCount
131+
}
132+
}
133+
},
117134
token: fcmToken,
118135
};
119136
try {

0 commit comments

Comments
 (0)