OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 373 forks source link

[Bug]: getInAppMessages initWithContext error in android #1554

Open ajayranga opened 1 year ago

ajayranga commented 1 year ago

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?

Relevant log output

in images

Code of Conduct

ajayranga commented 1 year ago

plz check

jennantilla commented 1 year ago

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!

ajayranga commented 1 year ago

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

SiM07 commented 1 year ago

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

GiddyNaya commented 1 year ago

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

abo-rythm commented 12 months ago

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)

markui commented 11 months ago

@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.

nan-li commented 10 months ago

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!

xgenem commented 10 months ago

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)
burakcbdn commented 10 months ago

Same here with flutter also

demirbascoskun commented 10 months ago

Hi. Version 5.0.4 and there is still a problem.

nan-li commented 9 months ago

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?
KaizerDmitry commented 9 months ago

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 commented 9 months ago

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

gustavosorati commented 9 months ago

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",
karthick-dmg commented 9 months ago

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);
Svarto commented 9 months ago

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 commented 9 months ago

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

gustavosorati commented 8 months ago

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.

rshettyawaken commented 8 months ago

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

rshettyawaken commented 8 months ago

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
Mahmoudkhoder01 commented 7 months ago

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?

gustavosorati commented 7 months ago

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

justinhandley commented 7 months ago

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

NourAliAbdou commented 7 months ago

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

uasghar-smile commented 6 months ago

+1

Issue detail

tonyjimena commented 6 months ago

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 )

Oleg144101957 commented 4 months ago

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]
userlab-luissaavedra commented 3 months ago

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.

ukcasso commented 1 month ago

@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

Jeyoung-Park commented 1 month ago

i used global variables to check if onesignal is initialized.

  useEffect(() => {
    if (!isOneSignalInit) {
      OneSignal.Debug.setLogLevel(LogLevel.Verbose);
      OneSignal.initialize(ONESIGNAL_APP_ID);      
      setIsOneSignalInit(true);
    }
  }, [isOneSignalInit]);
  useEffect(() => {
    if (isOneSignalInit) {
      OneSignal.Notifications.addEventListener('click', event => {
        console.log({event});
      });
    }
  }, [isOneSignalInit]);
gustavosorati commented 1 week ago

i used global variables to check if onesignal is initialized.

  useEffect(() => {
    if (!isOneSignalInit) {
      OneSignal.Debug.setLogLevel(LogLevel.Verbose);
      OneSignal.initialize(ONESIGNAL_APP_ID);      
      setIsOneSignalInit(true);
    }
  }, [isOneSignalInit]);
  useEffect(() => {
    if (isOneSignalInit) {
      OneSignal.Notifications.addEventListener('click', event => {
        console.log({event});
      });
    }
  }, [isOneSignalInit]);

Yes, this works but I don't think it should be ideal, because your variable IsOneSignalInit variable will always return true, even if an error occurs during initialization, you can check this by passing no value during oneSignal initialization.

So far I have not found any solution other than starting outside my initial App.tsx function. If anyone has found it, please share it.