Skip to content

Commit c92d989

Browse files
committed
Use Dispatchers.Main.immediate when initializing FirebasePushNotifierImpl
This runs the initialization logic setting delegates immediately if already called from the main thread, instead of asynchronously
1 parent 2062b16 commit c92d989

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

kmpnotifier/src/iosMain/kotlin/com/mmk/kmpnotifier/firebase/FirebasePushNotifierImpl.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import com.mmk.kmpnotifier.notification.IosNotifier
77
import com.mmk.kmpnotifier.notification.NotifierManagerImpl
88
import com.mmk.kmpnotifier.notification.PushNotifier
99
import kotlinx.cinterop.ExperimentalForeignApi
10+
import kotlinx.coroutines.CoroutineScope
11+
import kotlinx.coroutines.Dispatchers
1012
import kotlinx.coroutines.MainScope
13+
import kotlinx.coroutines.SupervisorJob
1114
import kotlinx.coroutines.launch
1215
import platform.UIKit.UIApplication
1316
import platform.UIKit.registerForRemoteNotifications
@@ -22,18 +25,18 @@ internal class FirebasePushNotifierImpl : PushNotifier {
2225

2326
private val firebaseMessageDelegate by lazy { FirebaseMessageDelegate() }
2427

28+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
29+
2530
init {
26-
MainScope().launch {
27-
currentLogger.log("FirebasePushNotifier is initialized")
31+
scope.launch {
32+
currentLogger.log("FirebasePushNotifier is initializing")
2833
UNUserNotificationCenter.currentNotificationCenter().delegate =
2934
IosNotifier.NotificationDelegate()
3035
FIRMessaging.messaging().delegate = firebaseMessageDelegate
3136
UIApplication.sharedApplication.registerForRemoteNotifications()
3237
}
33-
3438
}
3539

36-
3740
override suspend fun getToken(): String? = suspendCoroutine { cont ->
3841
FIRMessaging.messaging().tokenWithCompletion { token, error ->
3942
cont.resume(token)

0 commit comments

Comments
 (0)