-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] 알림 세팅 및 요청 관련 알림 구현 #152
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,6 @@ | ||
package com.psr.psr.notification.dto | ||
|
||
data class Data( | ||
val relatedId: Long, | ||
val notiType: String | ||
) |
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,6 @@ | ||
package com.psr.psr.notification.dto | ||
|
||
data class FcmMessage ( | ||
val validate_only: Boolean, | ||
val message: Message | ||
) |
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.psr.psr.notification.dto | ||
|
||
data class Message( | ||
val notification: Notification, | ||
val token: String, | ||
val data: Data | ||
) |
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/com/psr/psr/notification/dto/NotiAssembler.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,48 @@ | ||
package com.psr.psr.notification.dto | ||
|
||
import com.psr.psr.notification.entity.NotificationType | ||
import com.psr.psr.notification.entity.PushNotification | ||
import com.psr.psr.user.entity.User | ||
import org.springframework.stereotype.Component | ||
|
||
|
||
@Component | ||
class NotiAssembler { | ||
fun toEntity(receiver: User, title: String, content: String, relatedId: Long, type: NotificationType): PushNotification { | ||
return PushNotification( | ||
user = receiver, | ||
title = title, | ||
content = content, | ||
relatedId = relatedId, | ||
type = type | ||
) | ||
} | ||
fun toMessageDTO(targetToken: String, title: String, body: String, relatedId: Long, notiType: String): Message { | ||
return Message( | ||
notification = toNotificationDTO(title, body), | ||
token = targetToken, | ||
data = toDataDTO(relatedId, notiType) | ||
) | ||
} | ||
|
||
fun toNotificationDTO(title: String, body: String): Notification { | ||
return Notification( | ||
title = title, | ||
body = body | ||
) | ||
} | ||
|
||
fun toDataDTO(relatedId: Long, notiType: String): Data { | ||
return Data( | ||
relatedId = relatedId, | ||
notiType = notiType | ||
) | ||
} | ||
|
||
fun makeMessage(targetToken: String, title: String, body: String, relatedId: Long, notiType: String): FcmMessage { | ||
return FcmMessage( | ||
validate_only = false, | ||
message = toMessageDTO(targetToken, title, body, relatedId, notiType) | ||
) | ||
} | ||
} |
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,6 @@ | ||
package com.psr.psr.notification.dto | ||
|
||
data class Notification( | ||
val title: String, | ||
val body: String | ||
) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/psr/psr/notification/entity/NotificationType.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 com.psr.psr.notification.entity | ||
|
||
enum class NotificationType { | ||
NEW_ORDER, | ||
CHANGED_ORDER_STATUS, | ||
TWO_MONTH_ORDER, | ||
CHAT | ||
} |
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
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/com/psr/psr/notification/repository/NotificationRepository.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,9 +1,9 @@ | ||
package com.psr.psr.notification.repository | ||
|
||
import com.psr.psr.notification.entity.Notification | ||
import com.psr.psr.notification.entity.PushNotification | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface NotificationRepository: JpaRepository<Notification, Long>, NotificationCustom { | ||
interface NotificationRepository: JpaRepository<PushNotification, Long>, NotificationCustom { | ||
} |
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
112 changes: 111 additions & 1 deletion
112
src/main/kotlin/com/psr/psr/notification/service/NotificationService.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,17 +1,127 @@ | ||
package com.psr.psr.notification.service | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.google.auth.oauth2.GoogleCredentials | ||
import com.psr.psr.global.Constant.NotiSentence.NotiSentence.NEW_ORDER_SENTENCE | ||
import com.psr.psr.global.Constant.NotiSentence.NotiSentence.TWO_MONTH_ORDER_SENTENCE | ||
import com.psr.psr.notification.dto.FcmMessage | ||
import com.psr.psr.notification.dto.NotiAssembler | ||
import com.psr.psr.notification.dto.NotificationListRes | ||
import com.psr.psr.notification.entity.NotificationType | ||
import com.psr.psr.notification.repository.NotificationRepository | ||
import com.psr.psr.order.entity.OrderStatus | ||
import com.psr.psr.user.entity.User | ||
import okhttp3.* | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.core.io.ClassPathResource | ||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class NotificationService( | ||
private val notificationRepository: NotificationRepository | ||
private val notificationRepository: NotificationRepository, | ||
private val notiAssembler: NotiAssembler, | ||
@Value("\${firebase.sendUrl}") private val sendUrl: String, | ||
private val objectMapper: ObjectMapper | ||
) { | ||
// 알림 목록 조회 | ||
fun getNotiList(user: User, pageable: Pageable): Page<NotificationListRes> { | ||
return notificationRepository.findNotificationByUserGroupByDate(user, pageable) | ||
} | ||
|
||
// 새로운 요청 알림 | ||
fun sendNewOrderNoti(productName: String, orderReceiver: User, ordererName: String, orderId: Long) { | ||
val messageBody = ordererName + NEW_ORDER_SENTENCE | ||
notificationRepository.save(notiAssembler.toEntity( | ||
orderReceiver, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.NEW_ORDER | ||
)) | ||
|
||
if (orderReceiver.deviceToken != null) { | ||
val message: FcmMessage = notiAssembler.makeMessage( | ||
orderReceiver.deviceToken!!, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.NEW_ORDER.name | ||
) | ||
sendMessage(objectMapper.writeValueAsString(message)) | ||
} | ||
} | ||
|
||
// 요청 상태 변경 알림 | ||
fun sendChangeOrderStatusNoti(productName: String, orderer: User, orderStatus: OrderStatus, orderId: Long) { | ||
val messageBody = orderStatus.notiSentence!! | ||
notificationRepository.save(notiAssembler.toEntity( | ||
orderer, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.CHANGED_ORDER_STATUS | ||
)) | ||
|
||
if (orderer.deviceToken != null) { | ||
val message: FcmMessage = notiAssembler.makeMessage( | ||
orderer.deviceToken!!, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.CHANGED_ORDER_STATUS.name | ||
) | ||
sendMessage(objectMapper.writeValueAsString(message)) | ||
} | ||
} | ||
|
||
// 2달 뒤 요청상태 입력 요망 알림 | ||
fun send2MonthOrderNoti(productName: String, orderer: User, ordererName: String, orderId: Long) { | ||
val messageBody = ordererName + TWO_MONTH_ORDER_SENTENCE | ||
notificationRepository.save(notiAssembler.toEntity( | ||
orderer, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.TWO_MONTH_ORDER | ||
)) | ||
|
||
if (orderer.deviceToken != null) { | ||
val message: FcmMessage = notiAssembler.makeMessage( | ||
orderer.deviceToken!!, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.TWO_MONTH_ORDER.name | ||
) | ||
sendMessage(objectMapper.writeValueAsString(message)) | ||
} | ||
} | ||
|
||
// firebase accessToken 발급 | ||
private fun getAccessToken(): String? { | ||
val firebaseConfigPath = "firebase-service-key.json" | ||
val googleCredentials = GoogleCredentials | ||
.fromStream(ClassPathResource(firebaseConfigPath).inputStream) | ||
.createScoped(listOf("https://www.googleapis.com/auth/cloud-platform")) | ||
googleCredentials.refreshIfExpired() | ||
return googleCredentials.accessToken.tokenValue | ||
} | ||
|
||
// 메세지 전송 | ||
private fun sendMessage(message: String): Response { | ||
val client = OkHttpClient() | ||
val requestBody: RequestBody = message.toRequestBody("application/json; charset=utf-8".toMediaType()) | ||
val request: Request = Request.Builder() | ||
.url(sendUrl) | ||
.post(requestBody) | ||
.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + getAccessToken()) | ||
.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; UTF-8") | ||
.build() | ||
return client.newCall(request).execute() | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏👏👏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤍