-
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.
- Loading branch information
1 parent
28364ec
commit 2d93c78
Showing
7 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/ohayo/moyamoya/api/fcm/FCMTokenAddRequest.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 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
22
src/main/kotlin/com/ohayo/moyamoya/api/fcm/FcmController.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,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) | ||
} |
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,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() | ||
) | ||
} | ||
} | ||
} |
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,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
10
src/main/kotlin/com/ohayo/moyamoya/core/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,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> | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/ohayo/moyamoya/global/UserSessionHolder.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,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") | ||
} |