푸시메시지 기능 추가 - 전체, 개별, 라이브 생성, 라이브 시작, 메시지 전송, 콘텐츠 업로드
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package kr.co.vividnext.sodalive.fcm
|
||||
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/push")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
class FcmController(private val applicationEventPublisher: ApplicationEventPublisher) {
|
||||
@PostMapping
|
||||
fun send(
|
||||
@RequestBody request: PushRequest
|
||||
) = run {
|
||||
if (request.memberIds.isNotEmpty()) {
|
||||
applicationEventPublisher.publishEvent(
|
||||
FcmEvent(
|
||||
type = FcmEventType.INDIVIDUAL,
|
||||
title = request.title,
|
||||
message = request.message,
|
||||
recipients = request.memberIds
|
||||
)
|
||||
)
|
||||
} else {
|
||||
applicationEventPublisher.publishEvent(
|
||||
FcmEvent(
|
||||
type = FcmEventType.ALL,
|
||||
title = request.title,
|
||||
message = request.message,
|
||||
container = "ios"
|
||||
)
|
||||
)
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
FcmEvent(
|
||||
type = FcmEventType.ALL,
|
||||
title = request.title,
|
||||
message = request.message,
|
||||
container = "aos"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
137
src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt
Normal file
137
src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt
Normal file
@@ -0,0 +1,137 @@
|
||||
package kr.co.vividnext.sodalive.fcm
|
||||
|
||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import org.springframework.context.event.EventListener
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
enum class FcmEventType {
|
||||
ALL, INDIVIDUAL, CREATE_LIVE, START_LIVE, UPLOAD_CONTENT, SEND_MESSAGE
|
||||
}
|
||||
|
||||
class FcmEvent(
|
||||
val type: FcmEventType,
|
||||
val title: String,
|
||||
val message: String,
|
||||
val container: String = "",
|
||||
val recipients: List<Long> = listOf(),
|
||||
val isAuth: Boolean = false,
|
||||
val roomId: Long? = null,
|
||||
val contentId: Long? = null,
|
||||
val messageId: Long? = null,
|
||||
val creatorId: Long? = null
|
||||
)
|
||||
|
||||
@Component
|
||||
class FcmSendListener(
|
||||
private val pushService: FcmService,
|
||||
private val memberRepository: MemberRepository
|
||||
) {
|
||||
@EventListener
|
||||
fun send(fcmEvent: FcmEvent) {
|
||||
when (fcmEvent.type) {
|
||||
FcmEventType.ALL -> {
|
||||
if (fcmEvent.container.isNotBlank()) {
|
||||
val pushTokens = memberRepository.getAllRecipientPushTokens(
|
||||
fcmEvent.isAuth,
|
||||
fcmEvent.container
|
||||
)
|
||||
|
||||
for (tokens in pushTokens) {
|
||||
pushService.send(
|
||||
tokens = tokens,
|
||||
title = fcmEvent.title,
|
||||
message = fcmEvent.message,
|
||||
container = fcmEvent.container
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FcmEventType.INDIVIDUAL -> {
|
||||
if (fcmEvent.recipients.isNotEmpty()) {
|
||||
val pushTokens = memberRepository.getIndividualRecipientPushTokens(
|
||||
recipients = fcmEvent.recipients,
|
||||
isAuth = fcmEvent.isAuth
|
||||
)
|
||||
|
||||
val iosPushTokens = pushTokens["ios"]
|
||||
val aosPushToken = pushTokens["aos"]
|
||||
|
||||
if (iosPushTokens != null) {
|
||||
for (tokens in iosPushTokens) {
|
||||
pushService.send(
|
||||
tokens = tokens,
|
||||
title = fcmEvent.title,
|
||||
message = fcmEvent.message,
|
||||
container = fcmEvent.container
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (aosPushToken != null) {
|
||||
for (tokens in aosPushToken) {
|
||||
pushService.send(
|
||||
tokens = tokens,
|
||||
title = fcmEvent.title,
|
||||
message = fcmEvent.message,
|
||||
container = fcmEvent.container
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FcmEventType.CREATE_LIVE, FcmEventType.START_LIVE -> {
|
||||
if (fcmEvent.container.isNotBlank()) {
|
||||
val pushTokens = memberRepository.getCreateLiveRoomNotificationRecipientPushTokens(
|
||||
creatorId = fcmEvent.creatorId!!,
|
||||
isAuth = fcmEvent.isAuth,
|
||||
container = fcmEvent.container
|
||||
)
|
||||
|
||||
for (tokens in pushTokens) {
|
||||
pushService.send(
|
||||
tokens = tokens,
|
||||
title = fcmEvent.title,
|
||||
message = fcmEvent.message,
|
||||
container = fcmEvent.container,
|
||||
roomId = fcmEvent.roomId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FcmEventType.UPLOAD_CONTENT -> {
|
||||
if (fcmEvent.container.isNotBlank()) {
|
||||
val pushTokens = memberRepository.getUploadContentNotificationRecipientPushTokens(
|
||||
creatorId = fcmEvent.creatorId!!,
|
||||
isAuth = fcmEvent.isAuth,
|
||||
container = fcmEvent.container
|
||||
)
|
||||
|
||||
for (tokens in pushTokens) {
|
||||
pushService.send(
|
||||
tokens = tokens,
|
||||
title = fcmEvent.title,
|
||||
message = fcmEvent.message,
|
||||
container = fcmEvent.container,
|
||||
contentId = fcmEvent.contentId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FcmEventType.SEND_MESSAGE -> {
|
||||
val response = memberRepository.getMessageRecipientPushToken(messageId = fcmEvent.messageId!!)
|
||||
|
||||
pushService.send(
|
||||
tokens = listOf(response.pushToken),
|
||||
title = fcmEvent.title,
|
||||
message = fcmEvent.message,
|
||||
container = response.container,
|
||||
messageId = fcmEvent.messageId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmService.kt
Normal file
39
src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmService.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
package kr.co.vividnext.sodalive.fcm
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessaging
|
||||
import com.google.firebase.messaging.MulticastMessage
|
||||
import org.springframework.scheduling.annotation.Async
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class FcmService {
|
||||
@Async
|
||||
fun send(
|
||||
tokens: List<String>,
|
||||
title: String,
|
||||
message: String,
|
||||
container: String,
|
||||
roomId: Long? = null,
|
||||
messageId: Long? = null,
|
||||
contentId: Long? = null
|
||||
) {
|
||||
val multicastMessage = MulticastMessage.builder()
|
||||
.putData("title", title)
|
||||
.putData("message", message)
|
||||
.addAllTokens(tokens)
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
FirebaseMessaging.getInstance().sendEachForMulticast(multicastMessage.build())
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
package kr.co.vividnext.sodalive.fcm
|
||||
|
||||
import com.querydsl.core.annotations.QueryProjection
|
||||
|
||||
data class GetMessageRecipientPushTokenResponse @QueryProjection constructor(
|
||||
val pushToken: String,
|
||||
val container: String
|
||||
)
|
@@ -0,0 +1,7 @@
|
||||
package kr.co.vividnext.sodalive.fcm
|
||||
|
||||
data class PushRequest(
|
||||
val memberIds: List<Long>,
|
||||
val title: String,
|
||||
val message: String
|
||||
)
|
Reference in New Issue
Block a user