Skip to content

Commit

Permalink
๐Ÿ”€ :: (#79) ๊ทธ๋ฃน์•Œ๋ฆผ devicetoken length ์ œํ•œ ์ถ”๊ฐ€
Browse files Browse the repository at this point in the history
๐Ÿ”€ :: (#79) ๊ทธ๋ฃน์•Œ๋ฆผ devicetoken length ์ œํ•œ ์ถ”๊ฐ€
  • Loading branch information
lyutvs authored May 11, 2023
2 parents 73aad4d + 81366d4 commit b17d79a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.v1servicenotification.error

class InvalidDeviceTokenLengthException private constructor(): NotificationException(ErrorCode.DEVICE_TOKEN_LENGTH) {

companion object {
@JvmField
val EXCEPTION = InvalidDeviceTokenLengthException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, 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
}
}

}

0 comments on commit b17d79a

Please sign in to comment.