sodalive-backend-spring-boot/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmService.kt

61 lines
1.9 KiB
Kotlin

package kr.co.vividnext.sodalive.fcm
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.MulticastMessage
import com.google.firebase.messaging.Notification
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
@Service
class FcmService {
private val logger = LoggerFactory.getLogger(this::class.java)
@Async
fun send(
tokens: List<String>,
title: String,
message: String,
container: String,
roomId: Long? = null,
messageId: Long? = null,
contentId: Long? = null
) {
if (tokens.isNotEmpty()) {
logger.info("os: $container")
val multicastMessage = MulticastMessage.builder()
.addAllTokens(tokens)
if (container == "ios") {
multicastMessage
.setNotification(
Notification.builder()
.setTitle(title)
.setBody(message)
.build()
)
} else {
multicastMessage
.putData("title", title)
.putData("message", message)
}
if (roomId != null) {
multicastMessage.putData("room_id", roomId.toString())
}
if (messageId != null) {
multicastMessage.putData("message_id", messageId.toString())
}
if (contentId != null) {
multicastMessage.putData("content_id", contentId.toString())
}
val response = FirebaseMessaging.getInstance().sendEachForMulticast(multicastMessage.build())
logger.info("보내기 성공: ${response.successCount}")
logger.info("보내기 실패: ${response.failureCount}")
}
}
}