From 4d2211ef804480a3fa87d47d122f1c00fc6bf6c4 Mon Sep 17 00:00:00 2001 From: Oleg Yakovlev <64006320+oyakovlev@users.noreply.github.com> Date: Fri, 31 May 2024 17:01:56 +0700 Subject: [PATCH] added firebase messaging service late init for test supports (#17) --- README.md | 2 +- fcm-push-service/build.gradle.kts | 2 +- .../icerockdev/service/fcmpush/FCMConfig.kt | 3 ++- .../icerockdev/service/fcmpush/PushService.kt | 26 ++++++++++++++----- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7acf293..9b161a8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ repositories { } // Append dependency -implementation("com.icerockdev.service:fcm-push-service:2.1.1") +implementation("com.icerockdev.service:fcm-push-service:2.1.2") ```` ## Koin configure diff --git a/fcm-push-service/build.gradle.kts b/fcm-push-service/build.gradle.kts index 6819ada..7749414 100644 --- a/fcm-push-service/build.gradle.kts +++ b/fcm-push-service/build.gradle.kts @@ -5,7 +5,7 @@ import kotlin.text.String */ group = "com.icerockdev.service" -version = "2.1.1" +version = "2.1.2" plugins { id("org.jetbrains.kotlin.jvm") diff --git a/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/FCMConfig.kt b/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/FCMConfig.kt index a91d95b..4532ce9 100644 --- a/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/FCMConfig.kt +++ b/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/FCMConfig.kt @@ -5,5 +5,6 @@ package com.icerockdev.service.fcmpush data class FCMConfig( - val googleServiceAccountJson: String + val googleServiceAccountJson: String, + val lateInit: Boolean = false ) diff --git a/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/PushService.kt b/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/PushService.kt index 5b3445d..0ca3671 100644 --- a/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/PushService.kt +++ b/fcm-push-service/src/main/kotlin/com/icerockdev/service/fcmpush/PushService.kt @@ -24,18 +24,21 @@ import org.slf4j.LoggerFactory class PushService( private val coroutineScope: CoroutineScope, private val pushRepository: IPushRepository, - config: FCMConfig + private val config: FCMConfig ) { - private val firebaseMessaging: FirebaseMessaging + private lateinit var firebaseMessaging: FirebaseMessaging init { - val options = FirebaseOptions.builder() - .setCredentials(GoogleCredentials.fromStream(config.googleServiceAccountJson.byteInputStream())) - .build() - firebaseMessaging = FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options)) + if (!config.lateInit) { + initFirebaseMessagingService() + } } fun sendAsync(payLoad: FCMPayLoad): Deferred { + if (config.lateInit) { + initFirebaseMessagingService() + } + if (payLoad.tokenList.isEmpty()) { throw PushException("Unsupported empty token list") } @@ -164,6 +167,17 @@ class PushService( ) } + private fun initFirebaseMessagingService() { + if (this::firebaseMessaging.isInitialized) { + return + } + + val options = FirebaseOptions.builder() + .setCredentials(GoogleCredentials.fromStream(config.googleServiceAccountJson.byteInputStream())) + .build() + firebaseMessaging = FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options)) + } + private companion object { val logger: org.slf4j.Logger = LoggerFactory.getLogger(PushService::class.java) private const val FCM_TOKEN_CHUNK = 1000