Skip to content

Commit

Permalink
Fcm
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Jan 12, 2025
1 parent 28364ec commit 2d93c78
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ohayo.moyamoya.api.fcm

import com.fasterxml.jackson.annotation.JsonCreator

data class RegisterFcmTokenReq @JsonCreator constructor(
val fcmToken: String
)
22 changes: 22 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/api/fcm/FcmController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ohayo.moyamoya.api.fcm

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.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("fcm")
class FcmController(
private val fcmService: FcmService
) {
@PostMapping
fun register(@RequestBody req: RegisterFcmTokenReq) = fcmService.register(req)

@PostMapping("test/me")
fun testSend(
@RequestParam title: String,
@RequestParam body: String
) = fcmService.sendMessageToMe(title, body)
}
49 changes: 49 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/api/fcm/FcmService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ohayo.moyamoya.api.fcm

import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.Notification
import com.ohayo.moyamoya.core.FcmTokenEntity
import com.ohayo.moyamoya.core.FcmTokenRepository
import com.ohayo.moyamoya.global.UserSessionHolder
import org.springframework.stereotype.Service

@Service
class FcmService(
private val fcmTokenRepository: FcmTokenRepository,
private val fcmClient: FirebaseMessaging,
private val sessionHolder: UserSessionHolder
) {
fun register(req: RegisterFcmTokenReq) {
fcmTokenRepository.deleteByFcmToken(req.fcmToken)
fcmTokenRepository.save(
FcmTokenEntity(
fcmToken = req.fcmToken,
user = sessionHolder.current()
)
)
fcmClient.subscribeToTopic(listOf(req.fcmToken), "/topics/all")
}

fun sendMessageToMe(title: String, body: String) {
this.sendMessage(title, body, sessionHolder.current().id)
}

fun sendMessage(title: String, body: String, userId: Int) {
val tokens = fcmTokenRepository.findByUserId(userId)

tokens.forEach {
fcmClient.send(
Message.builder()
.setToken(it.fcmToken)
.setNotification(
Notification.builder()
.setTitle(title)
.setBody(body)
.build()
)
.build()
)
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/core/FcmTokenEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ohayo.moyamoya.core

import jakarta.persistence.*

@Entity
@Table(name = "tbl_fcm_token")
class FcmTokenEntity(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int = 0,

@Column(nullable = false)
val fcmToken: String,

@JoinColumn(nullable = false)
@ManyToOne(fetch = FetchType.LAZY)
val user: UserEntity,
)
10 changes: 10 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/core/FcmTokenRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ohayo.moyamoya.core

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository

@Repository
interface FcmTokenRepository: JpaRepository<FcmTokenEntity, Int> {
fun deleteByFcmToken(fcmToken: String)
fun findByUserId(userId: Int): List<FcmTokenEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.ohayo.moyamoya.core

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository

@Repository
interface PhoneCodeRepository: JpaRepository<PhoneCodeEntity, Int> {
@Query("SELECT m FROM PhoneCodeEntity m WHERE m.isActive = true AND m.phone = :phone AND m.code = :code")
fun findByIsActive(phone: String, code: String): List<PhoneCodeEntity>
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/com/ohayo/moyamoya/global/UserSessionHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ohayo.moyamoya.global

import com.ohayo.moyamoya.core.UserEntity
import com.ohayo.moyamoya.global.jwt.JwtUserDetails
import org.springframework.http.HttpStatus
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Component

@Component
class UserSessionHolder {
fun current(): UserEntity = (SecurityContextHolder.getContext().authentication.principal as? JwtUserDetails)?.user
?: throw CustomException(HttpStatus.NOT_FOUND, "Not found user")
}

0 comments on commit 2d93c78

Please sign in to comment.