File tree Expand file tree Collapse file tree
Firebase/functions/src/fcm Expand file tree Collapse file tree Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments