react-native-onesignal
react-native-onesignal copied to clipboard
[Bug]: NullPointerException on getPackageName() in OSUtils.getTargetSdkVersion
What happened?
In the react-native-onesignal package, we're encountering a java.lang.NullPointerException when calling android.content.Context.getPackageName() on a null object reference. The error specifically occurs at line 490 of OSUtils.java in the com.onesignal.OSUtils.getTargetSdkVersion method.
The stack trace indicates the error emerges when invoking the OneSignal.promptForPushNotifications method.
Stack trace:
com.onesignal.OSUtils.getTargetSdkVersion OSUtils.java:490 com.onesignal.NotificationPermissionController.
NotificationPermissionController.kt:48 com.onesignal.OneSignal.promptForPushNotifications OneSignal.java:2864 com.geektime.rnonesignalandroid.RNOneSignal.promptForPushNotificationsWithUserResponse RNOneSignal.java:473 java.lang.reflect.Method.invoke Method.java com.facebook.react.bridge.JavaMethodWrapper.invoke JavaMethodWrapper.java:372 com.facebook.react.bridge.JavaModuleWrapper.invoke JavaModuleWrapper.java:188 com.facebook.jni.NativeRunnable.run NativeRunnable.java android.os.Handler.handleCallback Handler.java:942 android.os.Handler.dispatchMessage Handler.java:99 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage MessageQueueThreadHandler.java:27 android.os.Looper.loopOnce Looper.java:226 android.os.Looper.loop Looper.java:313 com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run MessageQueueThreadImpl.java:228 java.lang.Thread.run Thread.java:1012
its appears to happen only on Android 13 devices.
Steps to reproduce?
Steps to reproduce:
its appears to happen only on Android 13 devices.
Call OneSignal.promptForPushNotifications method.
Observe the java.lang.NullPointerException in the logs.
The application crashes with a java.lang.NullPointerException when OneSignal.promptForPushNotifications is invoked.
What did you expect to happen?
The application should not crash when OneSignal.promptForPushNotifications is invoked.
React Native OneSignal SDK version
4.5.0
Which platform(s) are affected?
- [ ] iOS
- [X] Android
Relevant log output
com.onesignal.OSUtils.getTargetSdkVersion OSUtils.java:490
com.onesignal.NotificationPermissionController.<clinit> NotificationPermissionController.kt:48
com.onesignal.OneSignal.promptForPushNotifications OneSignal.java:2864
com.geektime.rnonesignalandroid.RNOneSignal.promptForPushNotificationsWithUserResponse RNOneSignal.java:473
java.lang.reflect.Method.invoke Method.java
com.facebook.react.bridge.JavaMethodWrapper.invoke JavaMethodWrapper.java:372
com.facebook.react.bridge.JavaModuleWrapper.invoke JavaModuleWrapper.java:188
com.facebook.jni.NativeRunnable.run NativeRunnable.java
android.os.Handler.handleCallback Handler.java:942
android.os.Handler.dispatchMessage Handler.java:99
com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage MessageQueueThreadHandler.java:27
android.os.Looper.loopOnce Looper.java:226
android.os.Looper.loop Looper.java:313
com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run MessageQueueThreadImpl.java:228
java.lang.Thread.run Thread.java:1012
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
Thank you for this information and steps to reproduce! We're investigating and will get back to you as soon as possible!
is there any update on this issue @jennantilla ?
this is happening to us, but on android 11
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: Work [ id=e7e00f8d-6d48-41fe-a83c-8450356624b7, tags={ FOCUS_LOST_WORKER_TAG, com.onesignal.OSFocusHandler$OnLostFocusWorker } ] failed because it threw an exception/error
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: java.util.concurrent.ExecutionException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:311)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at java.lang.Thread.run(Thread.java:923)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSUtils.packageInstalledAndEnabled(OSUtils.java:275)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSUtils.isGMSInstalledAndEnabled(OSUtils.java:288)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSUtils.supportsGooglePush(OSUtils.java:330)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSUtils.getDeviceType(OSUtils.java:349)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSUtils.isAndroidDeviceType(OSUtils.java:371)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.LocationController.isGooglePlayServicesAvailable(LocationController.java:337)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.LocationController.onFocusChange(LocationController.java:325)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OneSignal.onAppLostFocus(OneSignal.java:1345)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSFocusHandler$Companion.onLostFocusDoWork(OSFocusHandler.kt:139)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at com.onesignal.OSFocusHandler$OnLostFocusWorker.doWork(OSFocusHandler.kt:118)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: at androidx.work.Worker$1.run(Worker.java:86)
07-24 12:44:34.642 24057 24104 E WM-WorkerWrapper: ... 3 more
07-24 12:44:34.643 24057 24104 I WM-WorkerWrapper: Worker result FAILURE for Work [ id=e7e00f8d-6d48-41fe-a83c-8450356624b7, tags={ FOCUS_LOST_WORKER_TAG, com.onesignal.OSFocusHandler$OnLostFocusWorker } ]
@jbrodriguez is there any solution to prevent this error ?
@Shivemmer it seems sdk related, not sure it can be worked around, i opened another issue here https://github.com/OneSignal/react-native-onesignal/issues/1534
@jennantilla Any update?
Hi @jennantilla
It seems like it's been a whole quarter and still no solution :(
Hi @jennantilla Any update or timeframe on possible fix?
Any update? We're also facing this issue.
The problem is from this file
We need to move variable supportsNativePrompt to to block of code of the function prompt.
val supportsNativePrompt =
Build.VERSION.SDK_INT > 32 &&
OSUtils.getTargetSdkVersion(OneSignal.appContext) > 32.
Sometime, the application starts but the context was not initialized in OneSignal module soon enough and it will be null. And the block of above code will be executed when NotificationPermissionController is initialized --> Context is null. So it should be moved to block of code of the function prompt below. So when we call from JS promptForPushNotificationsWithUserResponse later, the context should be initialized. Hope some professionals will fix this soon. I will fix my own application using the patched library from my local and let you know the result. But I think my solution is right.
Hi @kyunkakata,
Could you please post the patch? Considering the fact that there has been no response since May 17th, the official fix won't be coming anytime soon.
Here is the pull request: https://github.com/OneSignal/OneSignal-Android-SDK/pull/1848 I cannot create local patch of OneSignal-Android-SDK. Their libs are required to sign .aar. So I create a pull request and hope that they will merge and create a patch for older version. My OneSignal-Android-SDK version I am using is 4.8.2
Will this get patched?
Same problem here.
Recently I had an error with OneSignal, when my application had started it crashed because of OneSignal. My error was different, but it can help anyone.
Error:
ViewRootImpl$CalledFromWrongThreadException
Only the original thread that created a view hierarchy can touch its views.
This error I get from Sentry.
My oneSignal version: react-native-onesignal": "^4.5.1"
To resolve this error I needed update the version of OneSignal to version 5 and my app stop to crash.
@jennantilla Can we possibly get an update on this? A pull request has been approved and merged into the SDK that is supposed to fix this issue but no activity on the React Native module yet.
Our app is live but crashes for all of our users on newer versions of Android and there's nothing we can tell our users other than "a fix is coming" but our hands are tied until this module is updated.
It's getting to the point now where our team is looking into alternatives to OneSignal :(
@jennantilla Can we possibly get an update on this? A pull request has been approved and merged into the SDK that is supposed to fix this issue but no activity on the React Native module yet.
Our app is live but crashes for all of our users on newer versions of Android and there's nothing we can tell our users other than "a fix is coming" but our hands are tied until this module is updated.
It's getting to the point now where our team is looking into alternatives to OneSignal :(
We had the same problem and just applied react-native-permissions requestNotifications (it works the same, use it instead of one signal promptForPushNotificationsWithUserResponse), maybe you can do that workaround until they fix this, that has been active for a long time.
Here is the pull request: OneSignal/OneSignal-Android-SDK#1848 I cannot create local patch of OneSignal-Android-SDK. Their libs are required to sign .aar. So I create a pull request and hope that they will merge and create a patch for older version. My OneSignal-Android-SDK version I am using is 4.8.2
This adjust was merged into the Android SDK, if I do a patch fix changing the version in build.gradle, should it resolve the issue? Is the react-native 4.5.2 library compatible with the android sdk com.onesignal:OneSignal:4.8.7?
https://github.com/fxamauri/react-native-onesignal/blob/main/android/build.gradle#L32
Please upgrade to https://github.com/OneSignal/react-native-onesignal/releases/tag/4.5.3 which Uses Android SDK 4.8.7+ https://github.com/OneSignal/OneSignal-Android-SDK/pull/1946