Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: App crashes on subsequent open on Android #1637

Closed
03balogun opened this issue Jan 28, 2024 · 4 comments
Closed

[Bug]: App crashes on subsequent open on Android #1637

03balogun opened this issue Jan 28, 2024 · 4 comments

Comments

@03balogun
Copy link

03balogun commented Jan 28, 2024

What happened?

When the app is installed and open for the first time it works fine, however force closing and re-opening leads to on open. Below is the error from Crashlytics.

image

Steps to reproduce?

1. install a reactnative app with

"expo": "~49.0.8"
"onesignal-expo-plugin": "^2.0.2",
"react-native-onesignal": "^5.0.3",
  1. Initialize Onesignal
  2. Open the app(Expectation is that it should work fine)
  3. Force close the app(swipe up from context menu or foreclose from settings)
  4. Reopen the app(App crashes)


### What did you expect to happen?

App should work with crashes

### React Native OneSignal SDK version

5.0.3

### Which platform(s) are affected?

- [ ] iOS
- [X] Android

### Relevant log output

```Shell
ERROR 1 (Detail on the first error from the Crashlytics screenshot):

Fatal Exception: java.lang.NullPointerException:
       at com.onesignal.internal.OneSignalImp.login(OneSignalImp.java:77)
       at com.onesignal.IOneSignal$DefaultImpls.login(IOneSignal.java:6)
       at com.onesignal.internal.OneSignalImp.login(OneSignalImp.java)
       at com.onesignal.OneSignal.login(OneSignal.java:11)
       at com.onesignal.rnonesignalandroid.RNOneSignal.login(RNOneSignal.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:148)
       at com.facebook.react.bridge.JavaModuleWrapper.invoke(:147)
       at com.facebook.jni.NativeRunnable.run(SourceFile)
       at android.os.Handler.handleCallback(Handler.java:874)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage()
       at android.os.Looper.loop(Looper.java:198)
       at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(:37)
       at java.lang.Thread.run(Thread.java:764)

Error 2 (Detail on the second error from the Crashlytics screenshot):

Caused by java.lang.Exception: Must call 'initWithContext' before use
       at com.onesignal.internal.OneSignalImp.getInAppMessages(OneSignalImp.java:16)
       at com.onesignal.OneSignal.getInAppMessages(OneSignal.java:6)
       at com.onesignal.rnonesignalandroid.RNOneSignal.removeHandlers(RNOneSignal.java)
       at com.onesignal.rnonesignalandroid.RNOneSignal.onHostDestroy(RNOneSignal.java)
       at com.facebook.react.bridge.ReactContext.onHostDestroy(ReactContext.java:25)
       at com.facebook.react.ReactInstanceManager.moveToBeforeCreateLifecycleState(ReactInstanceManager.java:26)
       at com.facebook.react.ReactInstanceManager.onHostDestroy(ReactInstanceManager.java:13)
       at com.facebook.react.ReactInstanceManager.onHostDestroy(ReactInstanceManager.java:4)
       at com.facebook.react.ReactDelegate.onHostDestroy(ReactDelegate.java:30)
       at com.facebook.react.ReactActivityDelegate.onDestroy(ReactActivityDelegate.java:2)
       at java.lang.reflect.Method.invoke(Method.java)
       at expo.modules.ReactActivityDelegateWrapper.invokeDelegateMethod(ReactActivityDelegateWrapper.java:35)
       at expo.modules.ReactActivityDelegateWrapper.onDestroy(ReactActivityDelegateWrapper.java:42)
       at com.facebook.react.ReactActivity.onDestroy(ReactActivity.java:5)
       at android.app.Activity.performDestroy(Activity.java:7395)
       at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1307)
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4470)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4503)
       at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:39)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:198)
       at android.app.ActivityThread.main(ActivityThread.java:6729)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)


### Code of Conduct

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

<!-- probot = {"onesignal-probot":{"response_time_in_business_days":0}} -->
@Zeyad-Slyndr
Copy link

Also having this issue

"expo": "~49.0.15"
"onesignal-expo-plugin": "^2.0.2",
"react-native-onesignal": "^5.0.4",

@opmat
Copy link

opmat commented Jan 30, 2024

I am using

"expo": "~49.0.15",
"onesignal-expo-plugin": "^2.0.2",
"react-native-onesignal": "^5.0.4",

but I'm not experiencing the issue.

What I have done is that I initialized the OneSignal early on app launch in my App.js as below

OneSignal.initialize(oneSignalAppId);

Check for permission next

const requestPermissions = async() => {
    // Also need enable notifications to complete OneSignal setup
    if (await OneSignal.Notifications.canRequestPermission()) {
      let permission = OneSignal.Notifications.hasPermission();
      if (!permission) {
        await OneSignal.Notifications.requestPermission(true);
      }
    }
    OneSignal.setConsentRequired(true);
    OneSignal.setConsentGiven(true);
  }

  useEffect(() => {
    requestPermissions();
  }, []);

then call login late during App load to allow it initialize and also used try...catch to track error

OneSignal.login(userData?.username.toLowerCase());

You can also subscribe to permissionChange event to determine when permission changes

I hope this helps

@jennantilla
Copy link
Contributor

jennantilla commented Feb 2, 2024

Hello everyone! This appears to be related to #1639 which is currently in review. Additional reports of this issue are being tracked in #1554 so for consolidation sake I'm going to close this issue and you can follow updates there. Please let me know if you believe this to be different and we will revisit. Thank you for your patience!

@03balogun
Copy link
Author

Thanks for the pointer @opmat

I initialized it in App.ts

// App.ts
OneSignal.initialize(oneSignalAppId);

Then request permission in a Root component with a slightly modified code.

// Root.ts
const requestOneSignalPermission = useCallback(async () => {
    try {
      OneSignal.Debug.setLogLevel(LogLevel.Verbose)

      if (await OneSignal.Notifications.canRequestPermission()) {
        const permission = OneSignal.Notifications.hasPermission()
        if (!permission) {
          OneSignal.Notifications.requestPermission(true).then(() => {
            OneSignal.login(user?.id || '')
            OneSignal.User.addEmail(user?.email || '')
          })
        }
      }
    } catch (e) {
      // Bugsnag.notify(e as Error)
    }
  }, [user])

  useEffect(() => {
    requestOneSignalPermission()
  }, [requestOneSignalPermission])

I hope this helps someone in the future 👊🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants