feat: 유저 행동 데이터 기록 추가 - 콘텐츠에 댓글 쓰기
This commit is contained in:
@@ -4,8 +4,6 @@ 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
|
||||
@@ -38,7 +36,7 @@ class FcmService(private val pushTokenService: PushTokenService) {
|
||||
|
||||
while (attempt <= maxAttempts && targets.isNotEmpty()) {
|
||||
val multicastMessage = MulticastMessage.builder()
|
||||
.addAllTokens(tokens)
|
||||
.addAllTokens(targets)
|
||||
|
||||
multicastMessage.setAndroidConfig(
|
||||
AndroidConfig.builder()
|
||||
@@ -117,51 +115,69 @@ class FcmService(private val pushTokenService: PushTokenService) {
|
||||
}
|
||||
}
|
||||
|
||||
fun sendPointGranted(token: String, point: Int) {
|
||||
fun sendPointGranted(tokens: List<String>, point: Int) {
|
||||
if (tokens.isEmpty()) return
|
||||
val data = mapOf(
|
||||
"type" to "POINT_GRANTED",
|
||||
"point" to point.toString(),
|
||||
"message" to "${point}포인트가 지급되었습니다!"
|
||||
)
|
||||
|
||||
var targets = tokens
|
||||
var attempts = 0
|
||||
val maxAttempts = 3
|
||||
|
||||
while (attempts < maxAttempts) {
|
||||
try {
|
||||
val message = Message.builder()
|
||||
.setToken(token)
|
||||
.putAllData(data)
|
||||
while (attempts <= maxAttempts && targets.isNotEmpty()) {
|
||||
val multicastMessage = MulticastMessage.builder()
|
||||
.addAllTokens(targets)
|
||||
.putAllData(data)
|
||||
|
||||
multicastMessage.setAndroidConfig(
|
||||
AndroidConfig.builder()
|
||||
.setPriority(AndroidConfig.Priority.HIGH)
|
||||
.build()
|
||||
)
|
||||
|
||||
val response = FirebaseMessaging.getInstance().send(message)
|
||||
logger.info("[FCM] ✅ 성공 (attempt ${attempts + 1}): messageId=$response")
|
||||
return // 성공 시 즉시 종료
|
||||
} catch (e: FirebaseMessagingException) {
|
||||
attempts++
|
||||
multicastMessage.setApnsConfig(
|
||||
ApnsConfig.builder()
|
||||
.setAps(
|
||||
Aps.builder()
|
||||
.setSound("default")
|
||||
.build()
|
||||
)
|
||||
.build()
|
||||
)
|
||||
|
||||
// "registration-token-not-registered" 예외 코드 확인
|
||||
if (e.messagingErrorCode == MessagingErrorCode.UNREGISTERED) {
|
||||
logger.error("[FCM] ❌ 실패: 토큰이 등록되지 않음 (등록 해제됨) → 재시도 안함")
|
||||
// DB에서 삭제
|
||||
pushTokenService.unregisterInvalidToken(token)
|
||||
return
|
||||
}
|
||||
val response = FirebaseMessaging.getInstance().sendEachForMulticast(multicastMessage.build())
|
||||
val failedTokens = mutableListOf<String>()
|
||||
|
||||
logger.error("[FCM] ❌ 실패 (attempt $attempts): ${e.errorCode} - ${e.message}")
|
||||
response.responses.forEachIndexed { index, res ->
|
||||
if (!res.isSuccessful) {
|
||||
val exception = res.exception
|
||||
val token = targets[index]
|
||||
|
||||
if (attempts >= maxAttempts) {
|
||||
logger.error("[FCM] ❌ 최종 실패: 전송 불가")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// Firebase 이외의 예외도 잡기
|
||||
attempts++
|
||||
logger.error("[FCM] ❌ 실패 (attempt $attempts): ${e.message}")
|
||||
|
||||
if (attempts >= maxAttempts) {
|
||||
logger.error("[FCM] ❌ 최종 실패: 알 수 없는 오류")
|
||||
if (exception?.messagingErrorCode == MessagingErrorCode.UNREGISTERED) {
|
||||
logger.error("[FCM] ❌ UNREGISTERED → $token")
|
||||
// DB에서 삭제
|
||||
pushTokenService.unregisterInvalidToken(token)
|
||||
} else {
|
||||
logger.error("[FCM] ❌ 실패: $token / ${exception?.messagingErrorCode}")
|
||||
failedTokens.add(token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failedTokens.isEmpty()) {
|
||||
logger.info("[FCM] ✅ 전체 전송 성공")
|
||||
return
|
||||
}
|
||||
|
||||
targets = failedTokens
|
||||
attempts++
|
||||
}
|
||||
|
||||
if (targets.isNotEmpty()) {
|
||||
logger.error("[FCM] ❌ 최종 실패 대상 ${targets.size}명 → $targets")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user