-
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.
Browse files
Browse the repository at this point in the history
[feat] 알림 세팅 및 요청 관련 알림 구현
- Loading branch information
Showing
19 changed files
with
251 additions
and
26 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
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
117 changes: 116 additions & 1 deletion
117
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,132 @@ | ||
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 (isPushNotiAvailable(orderReceiver)) { | ||
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 (isPushNotiAvailable(orderer)) { | ||
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 (isPushNotiAvailable(orderer)) { | ||
val message: FcmMessage = notiAssembler.makeMessage( | ||
orderer.deviceToken!!, | ||
productName, | ||
messageBody, | ||
orderId, | ||
NotificationType.TWO_MONTH_ORDER.name | ||
) | ||
sendMessage(objectMapper.writeValueAsString(message)) | ||
} | ||
} | ||
|
||
// 알림 수신 상태 체크 | ||
fun isPushNotiAvailable(user: User): Boolean { | ||
return user.deviceToken != null && user.notification | ||
} | ||
|
||
// 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.