OneSignal / react-native-onesignal

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

[Bug]: Crash "RuntimeException: Could not invoke OneSignal.hasNotificationPermission" #1681

Open audrius-sav opened 7 months ago

audrius-sav commented 7 months ago

What happened?

The latest OneSignal RN SDK v5.1.0 causes a RuntimeException and crashes the app when calling getPermissionAsync() in some Android devices according to our Sentry and Google Play crash analytics. Stack trace that's posted below suggests initWithContext is the culprit, but I'm sure we followed the OneSignal SDK setup thoroughly, and definitely call OneSignal.initialize with our appId on app startup. Interestingly, I cannot reproduce this crash on my physical OnePlus 8T device or Android 13 emulator, but some of my colleagues can, e.g. with Samsung Galaxy A14. Not sure about iOS.

Steps to reproduce?

Not sure how to reproduce, happens on some Android devices, e.g. Samsung Galaxy A14, Galaxy Tab A7, Galaxy A03, Moto E22, Galaxy A01, Redmi Note 8 and a lot others.

What did you expect to happen?

I expect the app to not crash when OneSignal.getPermissionAsync() is called.

React Native OneSignal SDK version

Release 5.1.0

Which platform(s) are affected?

Relevant log output

java.lang.Exception: Must call 'initWithContext' before use
    at com.onesignal.internal.OneSignalImp.getNotifications(OneSignalImp.kt:96)
    at com.onesignal.OneSignal.getNotifications(OneSignal.kt:62)
    at com.onesignal.rnonesignalandroid.RNOneSignal.hasNotificationPermission(RNOneSignal.java:460)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:149)
    at com.facebook.jni.NativeRunnable.run(NativeRunnable.java)
    at android.os.Handler.handleCallback(Handler.java:942)
    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:226)
    at android.os.Looper.loop(Looper.java:313)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
    at java.lang.Thread.run(Thread.java:1012)
java.lang.reflect.InvocationTargetException: null
    at java.lang.reflect.Method.invoke(Method.java)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:149)
    at com.facebook.jni.NativeRunnable.run(NativeRunnable.java)
    at android.os.Handler.handleCallback(Handler.java:942)
    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:226)
    at android.os.Looper.loop(Looper.java:313)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
    at java.lang.Thread.run(Thread.java:1012)
java.lang.RuntimeException: Could not invoke OneSignal.hasNotificationPermission
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:381)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:149)
    at com.facebook.jni.NativeRunnable.run(NativeRunnable.java)
    at android.os.Handler.handleCallback(Handler.java:942)
    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:226)
    at android.os.Looper.loop(Looper.java:313)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
    at java.lang.Thread.run(Thread.java:1012)

Code of Conduct

audrius-sav commented 7 months ago

Probably an issue on our side, didn't call initialize() in some cases. But still should it really cause a Runtime Exception and crash the app?

imiguez commented 5 months ago

I changed the initialize parameter from OneSignal.initialize(Constants.expoConfig.extra.oneSignalAppId) to OneSignal.initialize(process.env.EXPO_PUBLIC_ONE_SIGNAL_APP_ID) and the error disappeared.

123dma commented 3 months ago

same error here

JohnGeorgiadis commented 3 weeks ago

Same here with

    "react-native-onesignal": "^5.2.5",
    "onesignal-expo-plugin": "^2.0.3",

it affects mostly samsung devices