OneSignal / OneSignal-Android-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your native Android or Amazon app with OneSignal. https://onesignal.com
Other
604 stars 368 forks source link

[Bug]: OneSignal.removeTriggerForKey() attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference #1956

Closed chokokatana closed 9 months ago

chokokatana commented 10 months ago

What happened?

Remote crash report from app calling OneSignal.removeTriggerForKey in a viewModelScope.

Steps to reproduce?

Inside an activity, in the onStart() method instantiate a viewmodel and call removeTriggerForkey.

        viewModelScope.launch { OneSignal.removeTriggerForKey("in_home") }

What did you expect to happen?

No crash.

OneSignal Android SDK version

4.8.6

Android version

8

Specific Android models

* Transsion HOT 7

Relevant log output

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
       at com.onesignal.OneSignalDbHelper.getInstance(OneSignalDbHelper.java:132)
       at com.onesignal.OneSignal.getDBHelperInstance(OneSignal.java:3314)
       at com.onesignal.OneSignal.getInAppMessageController(OneSignal.java:458)
       at com.onesignal.OneSignal.removeTriggerForKey(OneSignal.java:3105)
       at com.geodb.wallace.presentation.splash.SplashViewModel$1.invokeSuspend(SplashViewModel.kt:87)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
       at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.java:110)
       at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
       at io.ktor.utils.io.ByteChannelCtorKt.ByteReadChannel(ByteChannelCtor.kt:56)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(BuildersKt__Builders_common.kt:56)
       at kotlinx.coroutines.BuildersKt.launch(Builders.kt:1)
       at com.geodb.wallace.kmm.utils.AndroidExceptionsKt.catchLaunch(AndroidExceptions.kt:23)
       at com.geodb.wallace.kmm.utils.AndroidExceptionsKt.catchLaunch$default(AndroidExceptions.kt:19)
       at com.geodb.wallace.presentation.splash.SplashViewModel.<init>(SplashViewModel.kt:87)
       at com.geodb.wallace.module.ViewModelModuleKt$viewModelModule$1$invoke$$inlined$viewModelOf$default$1.invoke(ViewModelModule.kt:232)
       at com.geodb.wallace.module.ViewModelModuleKt$viewModelModule$1$invoke$$inlined$viewModelOf$default$1.invoke(ViewModelModule.kt:120)
       at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:51)
       at org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:38)
       at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.java:116)
       at org.koin.core.scope.Scope.resolveValue(Scope.kt:246)
       at org.koin.core.scope.Scope.resolveInstance(Scope.kt:231)
       at org.koin.core.scope.Scope.get(Scope.kt:210)
       at org.koin.androidx.viewmodel.factory.KoinViewModelFactory.create(KoinViewModelFactory.kt:25)
       at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
       at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:153)
       at org.koin.androidx.viewmodel.GetViewModelKt.resolveViewModel(GetViewModel.kt:44)
       at org.koin.androidx.viewmodel.GetViewModelKt.resolveViewModel$default(GetViewModel.kt:29)
       at com.geodb.wallace.presentation.splash.SplashActivity$special$$inlined$viewModel$default$1.invoke(SplashActivity.java:46)
       at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
       at com.geodb.wallace.presentation.splash.SplashActivity.getMViewModel(SplashActivity.kt:38)
       at com.geodb.wallace.presentation.splash.SplashActivity.observeEvents(SplashActivity.kt:72)
       at com.geodb.wallace.presentation.base.BaseActivity.onStart(BaseActivity.kt:57)

Code of Conduct

jennantilla commented 9 months ago

@chokokatana thank you for reaching out! We're investigating this.

jennantilla commented 9 months ago

@chokokatana just wanted to confirm--where are you calling this method in your code? It appears that it may be getting called before OneSignal initializes. Are you able to send over your full log for us to dig deeper?

chokokatana commented 9 months ago

Can't sent the log due to not being reproducible. I'm initialising OneSignal in a background thread because otherwise I get ANR from users… so I guess this user managed to get to an activity with OneSignal init thread still stuck. Thanks for the idea, I can work around this and wrap all OneSignal calls to make sure the initialisation is done.