90 lines
2.8 KiB
Kotlin
90 lines
2.8 KiB
Kotlin
package kr.co.vividnext.sodalive.fcm
|
|
|
|
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.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,
|
|
creatorId: Long? = null,
|
|
auditionId: Long? = null
|
|
) {
|
|
if (tokens.isNotEmpty()) {
|
|
logger.info("os: $container")
|
|
val multicastMessage = MulticastMessage.builder()
|
|
.addAllTokens(tokens)
|
|
|
|
multicastMessage.setAndroidConfig(
|
|
AndroidConfig.builder()
|
|
.setPriority(AndroidConfig.Priority.HIGH)
|
|
.build()
|
|
)
|
|
|
|
multicastMessage.setApnsConfig(
|
|
ApnsConfig.builder()
|
|
.setAps(
|
|
Aps.builder()
|
|
.setSound("default")
|
|
.build()
|
|
)
|
|
.build()
|
|
)
|
|
|
|
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())
|
|
}
|
|
|
|
if (creatorId != null) {
|
|
multicastMessage.putData("channel_id", creatorId.toString())
|
|
}
|
|
|
|
if (auditionId != null) {
|
|
multicastMessage.putData("audition_id", auditionId.toString())
|
|
}
|
|
|
|
val response = FirebaseMessaging.getInstance().sendEachForMulticast(multicastMessage.build())
|
|
logger.info("보내기 성공: ${response.successCount}")
|
|
logger.info("보내기 실패: ${response.failureCount}")
|
|
}
|
|
}
|
|
}
|