-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into dependabot/gradle/org.springframework.boo…
…t-3.3.0
- Loading branch information
Showing
9 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,5 @@ out/ | |
### Kotlin ### | ||
.kotlin | ||
|
||
application-secret.properties | ||
application-secret.properties | ||
katchup_fcm.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/site/katchup/springboot/entity/FcmToken.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/kotlin/site/katchup/springboot/external/fcm/FcmConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/site/katchup/springboot/external/fcm/FcmRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/main/kotlin/site/katchup/springboot/external/fcm/FcmSender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/site/katchup/springboot/repository/FcmTokenRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters