Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/gradle/org.springframework.boo…
Browse files Browse the repository at this point in the history
…t-3.3.0
  • Loading branch information
unanchoi authored Jun 20, 2024
2 parents 5359ae4 + a1d0182 commit 7a89cf2
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ out/
### Kotlin ###
.kotlin

application-secret.properties
application-secret.properties
katchup_fcm.json
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ dependencies {
// mockk
val mockkVersion = "1.13.10"
testImplementation("io.mockk:mockk:$mockkVersion")

// fcm
implementation("com.google.firebase:firebase-admin:9.3.0")
}

tasks.withType<KotlinCompile> {
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/site/katchup/springboot/entity/FcmToken.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package site.katchup.springboot.entity

import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id

@Entity
class FcmToken(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
val token: String,
val memberId: Long,
)
31 changes: 31 additions & 0 deletions src/main/kotlin/site/katchup/springboot/external/fcm/FcmConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package site.katchup.springboot.external.fcm

import com.google.auth.oauth2.GoogleCredentials
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.messaging.FirebaseMessaging
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import javax.annotation.PostConstruct

@Configuration
class FcmConfig() {

lateinit var firebaseMessaging: FirebaseMessaging

@PostConstruct
fun init() {
val privateKey = ClassPathResource("katchup_fcm.json").inputStream
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(privateKey))
.build()
val app = FirebaseApp.initializeApp(options)
this.firebaseMessaging = FirebaseMessaging.getInstance(app)
}

@Bean
fun firebaseMessaging(): FirebaseMessaging {
return firebaseMessaging
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package site.katchup.springboot.external.fcm

data class FcmRequest(
val targetToken: String,
val title: String,
val content: String,
)
27 changes: 23 additions & 4 deletions src/main/kotlin/site/katchup/springboot/external/fcm/FcmSender.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package site.katchup.springboot.external.fcm

import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.Notification
import org.springframework.stereotype.Component
import site.katchup.springboot.exception.base.InternalServerException
import site.katchup.springboot.global.message.FailMessage

@Component
class FcmSender {

// TODO: implement FCM sending logic
fun send() {
class FcmSender(
private val firebaseMessaging: FirebaseMessaging,
) {
fun sendMessage(fcmRequest: FcmRequest) {
try {
val message = Message.builder()
.setToken(fcmRequest.targetToken)
.setNotification(
Notification.builder()
.setTitle(fcmRequest.title)
.setBody(fcmRequest.content)
.build(),
)
.build()
firebaseMessaging.sendAsync(message)
} catch (_: Exception) {
throw InternalServerException(FailMessage.FCM_SEND_ERROR)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ enum class FailMessage(
* 500 INTERNAL_SERVER_ERROR
*/
INTERNAL_SERVER_ERROR("서버 내부 오류"),
FCM_SEND_ERROR("FCM 전송 오류"),
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.katchup.springboot.repository

import org.springframework.data.jpa.repository.JpaRepository
import site.katchup.springboot.entity.FcmToken

interface FcmTokenRepository : JpaRepository<FcmToken, Long> {
fun findAllByMemberId(memberId: Long): List<FcmToken>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import site.katchup.springboot.dto.notification.request.NotificationRequest
import site.katchup.springboot.dto.notification.response.NotificationListResponse
import site.katchup.springboot.dto.notification.response.NotificationResponse
import site.katchup.springboot.entity.Notification
import site.katchup.springboot.external.fcm.FcmRequest
import site.katchup.springboot.external.fcm.FcmSender
import site.katchup.springboot.repository.FcmTokenRepository
import site.katchup.springboot.repository.NotificationRepository

@Service
@Transactional(readOnly = true)
class NotificationService(
private val notificationFinder: NotificationFinder,
private val notificationRepository: NotificationRepository,
private val fcmTokenRepository: FcmTokenRepository,
private val fcmSender: FcmSender,
) {

Expand All @@ -29,7 +32,16 @@ class NotificationService(
@Transactional
fun addNotification(memberId: Long, request: NotificationRequest) {
if (request.isPush) {
fcmSender.send()
fcmTokenRepository.findAllByMemberId(memberId)
.map {
fcmSender.sendMessage(
FcmRequest(
targetToken = it.token,
title = request.title,
content = request.content,
),
)
}
}
notificationRepository.save(
Notification(
Expand Down

0 comments on commit 7a89cf2

Please sign in to comment.