diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/ErrorCode.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/ErrorCode.kt index 3bb57967..03ec9d7e 100644 --- a/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/ErrorCode.kt +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/ErrorCode.kt @@ -21,4 +21,6 @@ enum class ErrorCode( DETAIL_NOT_FOUND(404, "DETAIL-404-1", "Detail Not Found."), INTERNAL_SERVER_ERROR(500, "GLOBAL-500-1", "Internal Server Error."), + + DEVICE_TOKEN_LENGTH(401, "DEVICE-400-1", "Device Token Length Error.") } diff --git a/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/InvalidDeviceTokenLengthException.kt b/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/InvalidDeviceTokenLengthException.kt new file mode 100644 index 00000000..6f8a8bf4 --- /dev/null +++ b/notification-domain/src/main/kotlin/io/github/v1servicenotification/error/InvalidDeviceTokenLengthException.kt @@ -0,0 +1,9 @@ +package io.github.v1servicenotification.error + +class InvalidDeviceTokenLengthException private constructor(): NotificationException(ErrorCode.DEVICE_TOKEN_LENGTH) { + + companion object { + @JvmField + val EXCEPTION = InvalidDeviceTokenLengthException() + } +} diff --git a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/global/fcm/FcmService.kt b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/global/fcm/FcmService.kt index 0c9596a3..2e0d0f5d 100644 --- a/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/global/fcm/FcmService.kt +++ b/notification-infrastructure/src/main/kotlin/io/github/v1servicenotification/global/fcm/FcmService.kt @@ -7,53 +7,66 @@ import com.google.firebase.messaging.Message import com.google.firebase.messaging.MulticastMessage import com.google.firebase.messaging.Notification import io.github.v1servicenotification.detail.spi.PostDetailFcmSpi +import io.github.v1servicenotification.error.InvalidDeviceTokenLengthException import org.springframework.stereotype.Service @Service class FcmService: PostDetailFcmSpi { + companion object { + const val MAX_TOKEN_LENGTH = 163 + } + override fun sendGroupMessage(tokenList: List, title: String, content: String, threadId: String) { - val multicast = MulticastMessage.builder() - .addAllTokens(tokenList) - .setNotification( - Notification.builder() - .setTitle(title) - .setBody(content) - .build() - ) - .setApnsConfig( - ApnsConfig.builder() - .setAps( - Aps.builder() - .setSound("default") - .setThreadId(threadId) - .build() - ).build() - ) - .build() + val validTokens = tokenList.filter { it.length == MAX_TOKEN_LENGTH } + if (validTokens.isNotEmpty()) { + val multicast = MulticastMessage.builder() + .addAllTokens(validTokens) + .setNotification( + Notification.builder() + .setTitle(title) + .setBody(content) + .build() + ) + .setApnsConfig( + ApnsConfig.builder() + .setAps( + Aps.builder() + .setSound("default") + .setThreadId(threadId) + .build() + ).build() + ) + .build() - FirebaseMessaging.getInstance().sendMulticastAsync(multicast) + FirebaseMessaging.getInstance().sendMulticastAsync(multicast) + } } override fun sendMessage(token: String, title: String, content: String, threadId: String) { - val message = Message.builder() - .setToken(token) - .setNotification( - Notification.builder() - .setTitle(title) - .setBody(content) - .build() - ) - .setApnsConfig( - ApnsConfig.builder() - .setAps( - Aps.builder() - .setSound("default") - .setThreadId(threadId) - .build() - ).build() - ) - .build() - FirebaseMessaging.getInstance().sendAsync(message) + if (token.length >= MAX_TOKEN_LENGTH) { + val message = Message.builder() + .setToken(token) + .setNotification( + Notification.builder() + .setTitle(title) + .setBody(content) + .build() + ) + .setApnsConfig( + ApnsConfig.builder() + .setAps( + Aps.builder() + .setSound("default") + .setThreadId(threadId) + .build() + ).build() + ) + .build() + FirebaseMessaging.getInstance().sendAsync(message) + } else { + throw InvalidDeviceTokenLengthException.EXCEPTION + } } + }