Skip to content

Commit

Permalink
added firebase messaging service late init for test supports (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
oyakovlev authored May 31, 2024
1 parent 288f5ea commit 4d2211e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fcm-push-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
package com.icerockdev.service.fcmpush

data class FCMConfig(
val googleServiceAccountJson: String
val googleServiceAccountJson: String,
val lateInit: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<PushResult> {
if (config.lateInit) {
initFirebaseMessagingService()
}

if (payLoad.tokenList.isEmpty()) {
throw PushException("Unsupported empty token list")
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4d2211e

Please sign in to comment.