-
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 pull request #5 from team-onui/develop
🌟 :: fcm
- Loading branch information
Showing
14 changed files
with
152 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
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
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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/example/onui/global/env/FCMProperty.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,11 @@ | ||
package com.example.onui.global.env | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.ConstructorBinding | ||
|
||
@ConstructorBinding | ||
@ConfigurationProperties("fcm.key") | ||
data class FCMProperty( | ||
val path: String, | ||
val scope: String | ||
) |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/com/example/onui/infra/fcm/FCMScheduling.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,23 @@ | ||
package com.example.onui.infra.fcm | ||
|
||
import com.example.onui.domain.user.repository.UserRepository | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class FCMScheduling( | ||
private val notificationService: NotificationService, | ||
private val userRepository: UserRepository | ||
) { | ||
|
||
@Transactional | ||
@Scheduled(cron = "0 20 8 * * ?", zone = "Asia/Seoul") | ||
fun essentailHost() { | ||
userRepository.findAll().parallelStream().peek { | ||
notificationService.sendByTokenList( | ||
it.deviceToken, "감정 기록하실 때가 됐어요!", "사용자님 오늘도 오누이에 감정을 기록해보아요!!" | ||
) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/example/onui/infra/fcm/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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.onui.infra.fcm | ||
|
||
interface NotificationService { | ||
|
||
fun sendByTokenList(tokenList: MutableSet<String>, title: String, body: String) | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/example/onui/infra/fcm/NotificationServiceImpl.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,56 @@ | ||
package com.example.onui.infra.fcm | ||
|
||
import com.example.onui.global.env.FCMProperty | ||
import com.google.auth.oauth2.GoogleCredentials | ||
import com.google.firebase.FirebaseApp | ||
import com.google.firebase.FirebaseOptions | ||
import com.google.firebase.messaging.* | ||
import mu.KotlinLogging | ||
import org.springframework.core.io.ClassPathResource | ||
import org.springframework.stereotype.Service | ||
import java.io.IOException | ||
import java.time.LocalDateTime | ||
import javax.annotation.PostConstruct | ||
|
||
@Service | ||
class NotificationServiceImpl( | ||
private val fcmProperty: FCMProperty | ||
) : NotificationService { | ||
|
||
private companion object { | ||
val logger = KotlinLogging.logger { } | ||
} | ||
|
||
@PostConstruct | ||
fun init() { | ||
try { | ||
val options = FirebaseOptions.builder() | ||
.setCredentials( | ||
GoogleCredentials | ||
.fromStream(ClassPathResource(fcmProperty.path).getInputStream()) | ||
.createScoped(listOf(fcmProperty.scope)) | ||
) | ||
.build() | ||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options) | ||
} | ||
} catch (e: IOException) { | ||
logger.error{ e.message } | ||
throw RuntimeException(e.message) | ||
} | ||
} | ||
|
||
// 알림 보내기 | ||
override fun sendByTokenList(tokenList: MutableSet<String>, title: String, body: String) { | ||
|
||
val messages: MutableList<Message> = tokenList.stream().map { | ||
Message.builder() | ||
.putData("time", LocalDateTime.now().toString()) | ||
.setNotification(Notification.builder().setTitle(title).setBody(body).build()) | ||
.setToken(it) | ||
.build() | ||
}.toList() | ||
|
||
FirebaseMessaging.getInstance().sendAll(messages) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/example/onui/infra/fcm/dto/reqeust/FCMNotificationRequest.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,6 @@ | ||
package com.example.onui.infra.fcm.dto.reqeust | ||
|
||
data class FCMNotificationRequest ( | ||
val title: String, | ||
val message: 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
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 @@ | ||
{ | ||
"type": "service_account", | ||
"project_id": "onui-399708", | ||
"private_key_id": "ed3eeefe4fd58acbb799bee0b440c68265331a44", | ||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDrmqmC9DZIMt4G\ndBh6u4D7M5u9tx3rgfLWZw5XIM0q7R9ax0HrEJZ+kfxO+5qIBDjdyBZnvZtvV8in\n/LIbj/zhBUrMu3uU4nHen03o/so9dgEzW8j8ln2PJHUz3DorpkzfmUQakdUgbOgE\nJxJSTHltVVZgOBiyItB3U6bO3EgWUSw64SI96sqc/jHXhzXHwJFC/xZNatgxJqKb\nqzhflwurC9a1VTZVaxipxsV4GqB/OlmvMSp4RvbWsUTOzI94+2QMrv0LtNnEj4WJ\nV3WOBf4+KEXpnGEQe26I3pzx5mpJ6kr5FbRjjyHyytu702klYsiu4amxUQmSU1Dj\n7HdaAG/LAgMBAAECggEAbCc03mEF1BHJuxOrMxgE5wuzWB0J1pTyKTroqdVsaWKt\nrInguCwGsbaJKKa2Mu8hPan+owO2qR+WhIrrJdzsvE0mH6KG4fsrQ4NEjAr+QsV7\nWpytQEpC/CVDyhkz+Nqf2lrsmPfN6tMjlNhswCvL6AUvH/9QUuHJaaWUYsxbKSab\ng/3m/FBRAAnst5cKupC1CTfh8eN+DYAsawA+/TxIiSWSr6gXItUyvOrAXvn7uegs\nwbb1SzKQp7ZwHHgF261LNjPuPilrJc7v9zI8TAr0aoje4u2Qxy3x08ScTG/Z6odM\n2PAJgaSPTOUEZQL7oG/IStyascZbVktO5u33ijsnKQKBgQD8Wyw/kQ+Tgn5XmfyW\nBJWjL1bx00mAY9OSRlUZiEvWJVacHivY1PS74h7e45dSzrdk3NvP7SEPxuXFLj1V\nGVveq3VLBIjqc0EzopAigtSOy+gmqQRi9CbvNR/aL/0xO1sH27hvuwF6+2edJAJs\nao9xeyf4alLhnqcbQkKu90mKUwKBgQDvAZDodbIvRrBvPlbUQGEZiOUxCNqm5HSL\namMeXVgAJvNyTs7wH4IP7B5bbK6KxmCQeLty8EZJRkEKNzvSGRrPVa96b3/Tduqm\n0uUPd+6g79VghECJLm+kZo9VUFkXrg6sh+yAqArN3x7aLqm08goSJ+TbE4tz/66z\neXdRdy2FqQKBgFj+rjQcrCmRM40hOPqO01ahM0BFCv7ENbC4LPq8HkJ/GHQmD8CA\nGW7I56ojCRKi4/omCT7imW4+7nkDPY3tS4DTZqH2D3LfAnd7NOl2yg8xycUYhft/\nrttdGMaRdfEOxaOX0QtWH8eHMZsxP8mMKtoSXJ42oNQAZ14tl+asPrsRAoGATaRq\nmLmzWtxR8LGFr4oCgTMRkW5Y6aKJoci7zl9weQwuRdIIM8VokReZfQW/ZeGv+P7f\nliUsEB28fz8WNdhl9zjUuqeCFQYqkGsucmn5oVqILMSJ2oa1SejvYz2o06J9rdqy\nH9F3QQ3cjfBevSNr1h4ToH+m69YBwNgSxOo8ZZECgYBDRSYUDj8KHFKsgrJW2it5\npFJvAdo0uZxt07lGPCUBHD4K2Ax441Q37G1zY7PSTwUhdX0mT+Nk/zehO3OEqcbm\nt0Dy4Q6UQBd7PQ1UZGrnMGgea4FU/ImQdOc3rPC5VrJYpifsl+JAXF5l5/y3J+4u\nUYoTahro+Tnm0haRCm355A==\n-----END PRIVATE KEY-----\n", | ||
"client_email": "[email protected]", | ||
"client_id": "107821224243694744575", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-jgwl3%40onui-399708.iam.gserviceaccount.com", | ||
"universe_domain": "googleapis.com" | ||
} |