mirzemehdi / KMPNotifier

Kotlin Multiplatform Push Notification Library targetting android, iOS, Desktop and Web (JS and Wasm)
http://mirzemehdi.com/KMPNotifier/
Apache License 2.0
340 stars 19 forks source link

Crash in android when receiving notification #53

Closed izBasit closed 3 months ago

izBasit commented 3 months ago

When a notification is received, I get the following crash on android

Caused by kotlin.UninitializedPropertyAccessException: lateinit property applicationContext has not been initialized at com.mmk.kmpnotifier.di.PlatformModule_androidKt.getApplicationContext(PlatformModule.android.kt:17) at com.mmk.kmpnotifier.di.PlatformModule_androidKt$platformModule$1$1.invoke(PlatformModule.android.kt:33) at com.mmk.kmpnotifier.di.PlatformModule_androidKt$platformModule$1$1.invoke(PlatformModule.android.kt:33) at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50) at org.koin.core.instance.SingleInstanceFactory.create(SingleInstanceFactory.kt:46) at org.koin.core.instance.SingleInstanceFactory$get$1.invoke(SingleInstanceFactory.kt:55) at org.koin.core.instance.SingleInstanceFactory$get$1.invoke(SingleInstanceFactory.kt:53) at org.koin.mp.KoinPlatformTools.synchronized(KoinPlatformTools.kt:36) at org.koin.core.instance.SingleInstanceFactory.get(SingleInstanceFactory.kt:53) at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:116) at org.koin.core.scope.Scope.resolveValue(Scope.kt:247) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:233) at org.koin.core.scope.Scope.get(Scope.kt:212) at org.koin.core.scope.Scope.get$default(Scope.kt:136) at com.mmk.kmpnotifier.di.PlatformModule_androidKt$platformModule$1$3.invoke(PlatformModule.android.kt:39) at com.mmk.kmpnotifier.di.PlatformModule_androidKt$platformModule$1$3.invoke(PlatformModule.android.kt:35) at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:50) at org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:38) at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:116) at org.koin.core.scope.Scope.resolveValue(Scope.kt:247) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:233) at org.koin.core.scope.Scope.get(Scope.kt:212) at org.koin.core.scope.Scope.get$default(Scope.kt:136) at com.mmk.kmpnotifier.notification.NotifierManagerImpl.getLocalNotifier(NotifierManagerImpl.kt:18) at com.mmk.kmpnotifier.firebase.MyFirebaseMessagingService$notifier$2.invoke(MyFirebaseMessagingService.kt:11) at com.mmk.kmpnotifier.firebase.MyFirebaseMessagingService$notifier$2.invoke(MyFirebaseMessagingService.kt:11) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.mmk.kmpnotifier.firebase.MyFirebaseMessagingService.getNotifier(MyFirebaseMessagingService.kt:11) at com.mmk.kmpnotifier.firebase.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.kt:21) at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(FirebaseMessagingService.java:237) at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(FirebaseMessagingService.java:187) at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(FirebaseMessagingService.java:174) at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(FirebaseMessagingService.java:163) at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0(EnhancedIntentService.java:82) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@18.1.0:2) at java.lang.Thread.run(Thread.java:1012)

From the looks of it, it seems that the way the lib is getting context for android via startup is not really working

mirzemehdi commented 3 months ago

@izBasit Please, for getting the problem give more context. 1)Which version of kmpnotifier you are using? 2) How do you initialize NotifierManager? 3)Check maybe in your manifest you disable auto startup initialization This code is usually added in your manifest for disabling auto startup initialization.


    <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        tools:node="remove" />
izBasit commented 3 months ago

It was number 3. Thanks @mirzemehdi for the quick response.

linhvnguyen9 commented 3 months ago

@mirzemehdi what do you think about making ContextInitializer public? I think some app have to use manual startup initialization logic (my app included). Maybe having an option to do so would be good?

mirzemehdi commented 3 months ago

@linhvnguyen9 yes, this is possible to be implemented. Agree with you that, there can be cases that automatic initialization is disabled

mirzemehdi commented 1 month ago

@linhvnguyen9

This is implemented in 1.3.0 version.

By default using androidx-startup Context instance is obtained automatically in android. But if you disabled androidx-startup, you can pass android Context instance using initialize method as below:


class MyApplication : Application() {
   override fun onCreate() {
       super.onCreate()
       NotifierManager.initialize(
           context = this
           configuration = NotificationPlatformConfiguration.Android(
               notificationIconResId = R.drawable.ic_launcher_foreground,
               showPushNotification = true,
           )
       )
   }
}