feat: 포인트 지급 시 FCM data-only 푸시 메시지 전송 및 실패 시 재시도 처리

This commit is contained in:
2025-04-22 17:35:47 +09:00
parent 51dae0f02c
commit 971683a81e
8 changed files with 111 additions and 14 deletions

View File

@@ -4,6 +4,9 @@ import com.google.firebase.messaging.AndroidConfig
import com.google.firebase.messaging.ApnsConfig
import com.google.firebase.messaging.Aps
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.FirebaseMessagingException
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.MessagingErrorCode
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Notification
import org.slf4j.LoggerFactory
@@ -82,8 +85,54 @@ class FcmService {
}
val response = FirebaseMessaging.getInstance().sendEachForMulticast(multicastMessage.build())
logger.info("보내기 성공: ${response.successCount}")
logger.info("보내기 실패: ${response.failureCount}")
logger.info("[FCM] ✅ 성공: ${response.successCount}")
logger.info("[FCM] ❌ 실패: ${response.failureCount}")
}
}
fun sendPointGranted(token: String, point: Int) {
val data = mapOf(
"type" to "POINT_GRANTED",
"point" to point.toString(),
"message" to "${point}포인트가 지급되었습니다!"
)
var attempts = 0
val maxAttempts = 3
while (attempts < maxAttempts) {
try {
val message = Message.builder()
.setToken(token)
.putAllData(data)
.build()
val response = FirebaseMessaging.getInstance().send(message)
logger.info("[FCM] ✅ 성공 (attempt ${attempts + 1}): messageId=$response")
return // 성공 시 즉시 종료
} catch (e: FirebaseMessagingException) {
attempts++
// "registration-token-not-registered" 예외 코드 확인
if (e.messagingErrorCode == MessagingErrorCode.UNREGISTERED) {
logger.error("[FCM] ❌ 실패: 토큰이 등록되지 않음 (등록 해제됨) → 재시도 안함")
return
}
logger.error("[FCM] ❌ 실패 (attempt $attempts): ${e.errorCode} - ${e.message}")
if (attempts >= maxAttempts) {
logger.error("[FCM] ❌ 최종 실패: 전송 불가")
}
} catch (e: Exception) {
// Firebase 이외의 예외도 잡기
attempts++
logger.error("[FCM] ❌ 실패 (attempt $attempts): ${e.message}")
if (attempts >= maxAttempts) {
logger.error("[FCM] ❌ 최종 실패: 알 수 없는 오류")
}
}
}
}
}