Skip to content

Commit

Permalink
Firebase initialization check
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Miller authored and Michael Miller committed Oct 25, 2022
1 parent 59d206d commit a64b17c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class CourierTests {
userId = COURIER_USER_ID,
title = "🐤 Chirp Chirp!",
body = "Message sent from Android Studio tests",
providers = listOf(CourierProvider.FCM)
providers = listOf(CourierProvider.FCM),
isProduction = false,
)

print("Request ID: $requestId")
Expand Down
10 changes: 9 additions & 1 deletion android/src/main/java/com/courier/android/Courier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.courier.android.models.CourierException
import com.courier.android.models.CourierProvider
import com.courier.android.repositories.TokenRepository
import com.courier.android.utils.NotificationEventBus
import com.google.firebase.FirebaseApp
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.RemoteMessage
import kotlinx.coroutines.*
Expand All @@ -20,7 +21,7 @@ class Courier private constructor() {
companion object {

var USER_AGENT = CourierAgent.NATIVE_ANDROID
internal const val VERSION = "1.0.20"
internal const val VERSION = "1.0.21"
internal const val TAG = "Courier SDK"
internal const val COURIER_PENDING_NOTIFICATION_KEY = "courier_pending_notification_key"
internal val eventBus by lazy { NotificationEventBus() }
Expand Down Expand Up @@ -204,6 +205,13 @@ class Courier private constructor() {

private suspend fun getCurrentFcmToken() = suspendCoroutine { continuation ->

// Check if we can get the FCM token
if (!::context.isInitialized || FirebaseApp.getApps(context).isEmpty()) {
Courier.log("Firebase is not initialized. Courier will not be able to get the FCM token until Firebase is initialized.")
continuation.resume(null)
return@suspendCoroutine
}

FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->

if (!task.isSuccessful) {
Expand Down

0 comments on commit a64b17c

Please sign in to comment.