react-native-onesignal icon indicating copy to clipboard operation
react-native-onesignal copied to clipboard

[Bug]: getInAppMessages initWithContext error in android

Open ajayranga opened this issue 2 years ago • 34 comments

What happened?

In debug mode in android getting initWithContext error even without initialization

Screenshot_20230912_123550_Cerebellum

Screenshot_20230912_123544_Cerebellum

Screenshot_20230912_123501_Cerebellum

Steps to reproduce?

install the package and got error
on samsung tab s6 lite android 13

What did you expect to happen?

on installing version 5.0.0

React Native OneSignal SDK version

5.0.0

Which platform(s) are affected?

  • [ ] iOS
  • [X] Android

Relevant log output

in images

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

ajayranga avatar Sep 12 '23 07:09 ajayranga

plz check

ajayranga avatar Sep 12 '23 07:09 ajayranga

Hello @ajayhcode thanks for reaching out! Could you please share more information about how you've initialized OneSignal in your app? If you could provide your initialization code as well as any additional log output prior to the error, we'd be happy to take a look!

jennantilla avatar Sep 14 '23 01:09 jennantilla

Everything was working fine earlier but this issue comes after upgrading the onesignal package and upgrading react native from 0.70.5 to 0.72.4

ajayranga avatar Sep 14 '23 04:09 ajayranga

Same error here, react-native-onesignal 5.0.0 and rn 0.72.3.

Link to same error with flutter https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/730

I initialize with :

import {OneSignal} from 'react-native-onesignal'; 
...
    OneSignal.setConsentRequired(false);
    OneSignal.setConsentGiven(true);
    OneSignal.initialize(Config.ONE_SIGNAL_APP_ID);

I remove onesignal in gradle and just keep react-native-onesignal with ^5.0.0

SiM07 avatar Sep 14 '23 12:09 SiM07

The issue went away after I downgraded to react-native-onesignal:4.5.2 with react-native 0.72.4

GiddyNaya avatar Sep 16 '23 01:09 GiddyNaya

The issue reproduced for me as well:

Your app just crashed. See the error below. java.lang.RuntimeException: Could not invoke OneSignal.hasNotificationPermission com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:383) ... Caused by java.lang.Exception: Must call 'initWithContext' before use com.onesignal.internal.OneSignalImp.getNotifications(OneSignalImp.kt:81) com.onesignal.OneSignal.getNotifications(OneSignal.kt:62)

abo-rythm avatar Oct 09 '23 19:10 abo-rythm

@jennantilla

Is there any progress?

I am also having this issue. https://documentation.onesignal.com/docs/react-native-sdk-setup docs say here that we need to also follow the steps for Android SDK setup. Do we need to follow all steps in android sdk? even when we are using react native sdk? The docs are not straightforward.

markui avatar Nov 10 '23 02:11 markui

Hi @markui, thanks for pointing out some issue with our setup instructions. There is no need to follow the Android setup steps, and we will remove those instructions.

The exception about Expected receiver of type com.amazon.device.iap.internal.d, but got kotlin.Unit has been fixed since Release 5.0.2.

For any other issues, please still refer to these instructions posted above..

Could you please share more information about how you've initialized OneSignal in your app? If you could provide your initialization code as well as any additional log output prior to the error, we'd be happy to take a look!

More information will help us investigate the issue, thanks for your help!

nan-li avatar Nov 13 '23 20:11 nan-li

Hope this helps...

Exception in native call
java.lang.Exception: Must call 'initWithContext' before use
	at com.onesignal.internal.OneSignalImp.getInAppMessages(OneSignalImp.kt:107)
	at com.onesignal.OneSignal.getInAppMessages(OneSignal.kt:78)
	at com.onesignal.rnonesignalandroid.RNOneSignal.removeHandlers(RNOneSignal.java:166)
	at com.onesignal.rnonesignalandroid.RNOneSignal.onCatalystInstanceDestroy(RNOneSignal.java:210)
	at com.facebook.react.bridge.BaseJavaModule.invalidate(BaseJavaModule.java:76)
	at com.facebook.react.bridge.ModuleHolder.destroy(ModuleHolder.java:110)
	at com.facebook.react.bridge.NativeModuleRegistry.notifyJSInstanceDestroy(NativeModuleRegistry.java:108)
	at com.facebook.react.bridge.CatalystInstanceImpl$1.run(CatalystInstanceImpl.java:359)
	at android.os.Handler.handleCallback(Handler.java:938)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228)
	at java.lang.Thread.run(Thread.java:920)

xgenem avatar Dec 01 '23 19:12 xgenem

Same here with flutter also

burakcbdn avatar Dec 07 '23 07:12 burakcbdn

Hi. Version 5.0.4 and there is still a problem.

demirbascoskun avatar Dec 09 '23 10:12 demirbascoskun

Hi @xgenem and @burakcbdn and @demirbascoskun,

Can you share more information about your report? This GitHub Issue has a few different issues mentioned, so please clarify what you are experiencing, please still refer to these instructions posted above..

Could you please share more information about how you've initialized OneSignal in your app? If you could provide your initialization code as well as any additional log output prior to the error, we'd be happy to take a look!

  1. What actions that led to the crash?
  2. Stacktrace
  3. Reproducible?
  4. SDK version?

nan-li avatar Dec 10 '23 03:12 nan-li

hello! @demirbascoskun @burakcbdn I know that it's dumb and probably you've check everything, but I had same issue and in my case I just missed APP_ID in my CI configuration, so it didn't initialized correctly and because next calls was on not initialized instance - it throw those exceptions. Maybe it will help...

KaizerDmitry avatar Dec 11 '23 08:12 KaizerDmitry

More insights. I also found out in my project that sometimes after codepush (when bundle restarting) onCatalystInstanceDestroy(RNOneSignal.java:210) function called, which trying to invoke function at instance that not exist. After upgrading to latest ("5.0.4") issue gone. P.S. I can see some changes related to this issue for Android SDK 5.0.5

KaizerDmitry avatar Dec 12 '23 14:12 KaizerDmitry

I also face the same problem, I initialize onesignal within a useEffect in App.tsx, and run the device in the emulator, if I press R for a quick reload, it seems to lose the onesignal initialization reference.

I can only get around this problem by initializing onesignal outside of app.tsx in an isolated function.

OneSignal.Debug.setLogLevel(LogLevel.Verbose);
OneSignal.initialize(env.ONE_SIGNAL_APP_ID_ANDROID);
OneSignal.Notifications.requestPermission(true);
    "react-native-onesignal": "^5.0.4",
    "onesignal-expo-plugin": "^2.0.2",

gustavosorati avatar Dec 22 '23 17:12 gustavosorati

Hope this helps...

Exception in native call
java.lang.Exception: Must call 'initWithContext' before use
	at com.onesignal.internal.OneSignalImp.getInAppMessages(OneSignalImp.kt:107)
	at com.onesignal.OneSignal.getInAppMessages(OneSignal.kt:78)
	at com.onesignal.rnonesignalandroid.RNOneSignal.removeHandlers(RNOneSignal.java:166)
	at com.onesignal.rnonesignalandroid.RNOneSignal.onCatalystInstanceDestroy(RNOneSignal.java:210)
	at com.facebook.react.bridge.BaseJavaModule.invalidate(BaseJavaModule.java:76)
	at com.facebook.react.bridge.ModuleHolder.destroy(ModuleHolder.java:110)
	at com.facebook.react.bridge.NativeModuleRegistry.notifyJSInstanceDestroy(NativeModuleRegistry.java:108)
	at com.facebook.react.bridge.CatalystInstanceImpl$1.run(CatalystInstanceImpl.java:359)
	at android.os.Handler.handleCallback(Handler.java:938)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228)
	at java.lang.Thread.run(Thread.java:920)

RNOneSignal.java

Screenshot 2024-01-04 at 12 38 47 PM

@nan-li I'm facing the same issue on some Android devices; this is how I've initialized OneSignal in my app.

const OneSignalConfig = () => {

  useEffect(() => {
    if (__DEV__) {
      OneSignal.Debug.setLogLevel(LogLevel.Verbose);
    }
    OneSignal.initialize(Config.ONE_SIGNAL_APP_ID);
    OneSignal.Notifications.requestPermission(true);
    OneSignal.Notifications.addEventListener('click', (event) => {
      const data = event?.notification?.additionalData as NotificationData;
      const deepLink = data?.deep_link || '';
      mixpanel.track('User Clicked Push Notification', {
        deep_link: deepLink,
        title: event?.notification?.title || '',
        body: event?.notification?.body || '',
        notification_id: event?.notification?.notificationId || '',
        platform: 'OneSignal'
      });
    });
  }, []);
  return null;
};

export default React.memo(OneSignalConfig);

karthick-dmg avatar Jan 04 '24 06:01 karthick-dmg

I also face the same problem, I initialize onesignal within a useEffect in App.tsx, and run the device in the emulator, if I press R for a quick reload, it seems to lose the onesignal initialization reference.

I can only get around this problem by initializing onesignal outside of app.tsx in an isolated function.

OneSignal.Debug.setLogLevel(LogLevel.Verbose);
OneSignal.initialize(env.ONE_SIGNAL_APP_ID_ANDROID);
OneSignal.Notifications.requestPermission(true);
    "react-native-onesignal": "^5.0.4",
    "onesignal-expo-plugin": "^2.0.2",

I have the same error and packages. Do you still call the isolated function inside a use effect in app.tsx or do you mean that you initialize One Signal outside of App.tsx function completely (i.e. in a global context instead)?

Svarto avatar Jan 06 '24 09:01 Svarto

What is rather peculiar is that I noticed that even when I do not initialize OneSignal (i.e. in development mode), it anyway tries to load the GetInAppMessages and that fires off this error in development.

Early testing indicates that @gustavosorati solution also works for me, I put the initialization in a separate function that is called from app.ts

Svarto avatar Jan 06 '24 18:01 Svarto

I have the same error and packages. Do you still call the isolated function inside a use effect in app.tsx or do you mean that you initialize One Signal outside of App.tsx function completely (i.e. in a global context instead)?

I currently initialize One Signal outside of the App.tsx function.

gustavosorati avatar Jan 09 '24 15:01 gustavosorati

While trying to reload our Expo app, this issue occurs without initializing OneSignal on Android.

rshettyawaken avatar Jan 11 '24 22:01 rshettyawaken

This is a complete report from PlayStore:

FATAL EXCEPTION: Thread-2
Process: com.awaken180weightloss.mobile, PID: 11043
java.lang.RuntimeException: Unable to destroy activity {com.awaken180weightloss.mobile/com.awaken180weightloss.mobile.MainActivity}: java.lang.reflect.InvocationTargetException
	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5454)
	at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5487)
	at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at androidx.test.espresso.base.Interrogator.loopAndInterrogate(Interrogator.java:10)
	at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:7)
	at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:1)
	at androidx.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:5)
	at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:6)
	at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:1)
	at androidx.test.espresso.action.Tap.sendSingleTap(Tap.java:5)
	at androidx.test.espresso.action.Tap.-$$Nest$smsendSingleTap(Unknown Source:0)
	at androidx.test.espresso.action.Tap$1.sendTap(Tap.java:1)
	at androidx.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:4)
	at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:2)
	at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:23)
	at androidx.test.espresso.ViewInteraction.-$$Nest$mdoPerform(Unknown Source:0)
	at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:6)
	at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:1)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at android.os.Handler.handleCallback(Handler.java:938)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7870)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.reflect.InvocationTargetException
	at java.lang.reflect.Method.invoke(Native Method)
	at expo.modules.ReactActivityDelegateWrapper.invokeDelegateMethod(ReactActivityDelegateWrapper.kt:285)
	at expo.modules.ReactActivityDelegateWrapper.onDestroy(ReactActivityDelegateWrapper.kt:191)
	at com.facebook.react.ReactActivity.onDestroy(ReactActivity.java:64)
	at android.app.Activity.performDestroy(Activity.java:8339)
	at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1376)
	at androidx.test.runner.MonitoringInstrumentation.callActivityOnDestroy(MonitoringInstrumentation.java:1)
	at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5441)
	... 31 more
Caused by: java.lang.Exception: Must call 'initWithContext' before use
	at com.onesignal.internal.OneSignalImp.getInAppMessages(OneSignalImp.kt:111)
	at com.onesignal.OneSignal.getInAppMessages(OneSignal.kt:78)
	at com.onesignal.rnonesignalandroid.RNOneSignal.removeHandlers(RNOneSignal.java:168)
	at com.onesignal.rnonesignalandroid.RNOneSignal.onHostDestroy(RNOneSignal.java:200)
	at com.facebook.react.bridge.ReactContext.onHostDestroy(ReactContext.java:351)
	at com.facebook.react.ReactInstanceManager.moveToBeforeCreateLifecycleState(ReactInstanceManager.java:805)
	at com.facebook.react.ReactInstanceManager.onHostDestroy(ReactInstanceManager.java:694)
	at com.facebook.react.ReactInstanceManager.onHostDestroy(ReactInstanceManager.java:710)
	at com.facebook.react.ReactDelegate.onHostDestroy(ReactDelegate.java:90)
	at com.facebook.react.ReactActivityDelegate.onDestroy(ReactActivityDelegate.java:132)
	... 39 more

rshettyawaken avatar Jan 19 '24 20:01 rshettyawaken

I also face the same problem, I initialize onesignal within a useEffect in App.tsx, and run the device in the emulator, if I press R for a quick reload, it seems to lose the onesignal initialization reference.

I can only get around this problem by initializing onesignal outside of app.tsx in an isolated function.

``` OneSignal.Debug.setLogLevel(LogLevel.Verbose); OneSignal.initialize(env.ONE_SIGNAL_APP_ID_ANDROID); OneSignal.Notifications.requestPermission(true); ```
    "react-native-onesignal": "^5.0.4",
    "onesignal-expo-plugin": "^2.0.2",

Did you solve your problem?

Mahmoudkhoder01 avatar Feb 15 '24 09:02 Mahmoudkhoder01

I also face the same problem, I initialize onesignal within a useEffect in App.tsx, and run the device in the emulator, if I press R for a quick reload, it seems to lose the onesignal initialization reference. I can only get around this problem by initializing onesignal outside of app.tsx in an isolated function.

OneSignal.Debug.setLogLevel(LogLevel.Verbose);
OneSignal.initialize(env.ONE_SIGNAL_APP_ID_ANDROID);
OneSignal.Notifications.requestPermission(true);
    "react-native-onesignal": "^5.0.4",
    "onesignal-expo-plugin": "^2.0.2",

Did you solve your problem?

I managed to get around it by starting in an external function outside of App.tsx

gustavosorati avatar Feb 21 '24 14:02 gustavosorati

@gustavosorati can you add some more context around how you are calling it / where you are including it in your code?

justinhandley avatar Feb 29 '24 13:02 justinhandley

updating react-native-onesignal to the latest version solves this problem.

NourAliAbdou avatar Mar 04 '24 08:03 NourAliAbdou

+1

Issue detail

uasghar-smile avatar Mar 12 '24 06:03 uasghar-smile

On my case the error comes because the constant of onesignalAppId is undefined when i use with Constants.expoConfig.extra.oneSignal App Id,

Im solved using process.env OneSignal.initialize( Constants.expoConfig.extra.oneSignalAppId || process.env.EXPO_PUBLIC_ONESIGNAL_APP_ID )

tonyjimena avatar Mar 12 '24 17:03 tonyjimena

The same problem Android Kotlin

                                                                                                Process: com.cristaltitans.gamedev, PID: 26794
                                                                                                java.lang.Exception: Must call 'initWithContext' before 'login'
                                                                                                	at com.onesignal.internal.OneSignalImp.login(OneSignalImp.kt:342)
                                                                                                	at com.onesignal.IOneSignal$DefaultImpls.login(IOneSignal.kt:117)
                                                                                                	at com.onesignal.internal.OneSignalImp.login(OneSignalImp.kt:56)
                                                                                                	at com.onesignal.OneSignal.login(OneSignal.kt:157)
                                                                                                	at com.cristaltitans.gamedev.start_screen.data.MainDataRepositoryImpl.getPushID(MainDataRepositoryImpl.kt:34)
                                                                                                	at com.cristaltitans.gamedev.start_screen.presentation.StartActivity$onCreate$1$defPushId$1.invokeSuspend(StartActivity.kt:31)
                                                                                                	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
                                                                                                	at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
                                                                                                	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:376)
                                                                                                	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.kt:110)
                                                                                                	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
                                                                                                	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
                                                                                                	at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
                                                                                                	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
                                                                                                	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
                                                                                                	at com.cristaltitans.gamedev.start_screen.presentation.StartActivity.onCreate(StartActivity.kt:30)
                                                                                                	at android.app.Activity.performCreate(Activity.java:8595)
                                                                                                	at android.app.Activity.performCreate(Activity.java:8573)
                                                                                                	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
                                                                                                	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
                                                                                                	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
                                                                                                	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
                                                                                                	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
                                                                                                	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
                                                                                                	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
                                                                                                	at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                	at android.os.Looper.loopOnce(Looper.java:205)
                                                                                                	at android.os.Looper.loop(Looper.java:294)
                                                                                                	at android.app.ActivityThread.main(ActivityThread.java:8177)
                                                                                                	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
                                                                                                	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
                                                                                                	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@324e9ed, Dispatchers.Main.immediate]

Oleg144101957 avatar May 20 '24 19:05 Oleg144101957

I'm facing the same issue

There is an issue related: https://github.com/OneSignal/react-native-onesignal/issues/1587

I checked how Onesignal is being initialized, as suggested in the issue #1587 I'm initializing Onesignal from a useEffect and not including initWithContext in MainApplication.onCreate(). But I'm still having the problem.

Apparently, the issue was solved in Android SDK 5.1.16 release (July 2, 2024) https://github.com/OneSignal/OneSignal-Android-SDK/releases/tag/5.1.16

And react-native-onesignal latest release updates Android SDK to version 5.1.15 (Jun 21, 2024) https://github.com/OneSignal/react-native-onesignal/releases/tag/5.2.1

So, wait for the next release that includes the update of Android SDK to version 5.1.16 or higher.

userlab-luissaavedra avatar Jul 03 '24 16:07 userlab-luissaavedra

@userlab-luissaavedra According to you, it was fixed in 5.2.2 version? I'll give it a try. https://github.com/OneSignal/react-native-onesignal/releases/tag/5.2.2

ukcasso avatar Aug 07 '24 01:08 ukcasso