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]: NullPointerException on getPackageName() in OSUtils.getTargetSdkVersion #1516

Open
2 of 3 tasks
maoryadin opened this issue May 16, 2023 · 19 comments
Open
2 of 3 tasks

Comments

@maoryadin
Copy link

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
  • 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

  • I agree to follow this project's Code of Conduct
@jennantilla
Copy link
Contributor

Thank you for this information and steps to reproduce! We're investigating and will get back to you as soon as possible!

@Shivemmer
Copy link

is there any update on this issue @jennantilla ?

@jbrodriguez
Copy link

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 } ]

@Shivemmer
Copy link

@jbrodriguez is there any solution to prevent this error ?

@jbrodriguez
Copy link

@Shivemmer it seems sdk related, not sure it can be worked around, i opened another issue here #1534

@Rohit3523
Copy link

@jennantilla Any update?

@markovdigital
Copy link

Hi @jennantilla

It seems like it's been a whole quarter and still no solution :(

@MahyarMM
Copy link

Hi @jennantilla
Any update or timeframe on possible fix?

@augustovicente
Copy link

Any update? We're also facing this issue.

@kyunkakata
Copy link

kyunkakata commented Sep 21, 2023

The problem is from this file
Screenshot 2023-09-21 at 14 40 14
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.

@markovdigital
Copy link

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.

@kyunkakata
Copy link

kyunkakata commented Sep 22, 2023

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

@joaquinvaz
Copy link

Will this get patched?

@mtebele
Copy link

mtebele commented Oct 6, 2023

Same problem here.

@BarbieriMatheus
Copy link

BarbieriMatheus commented Oct 27, 2023

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.

@kodie
Copy link

kodie commented Nov 10, 2023

@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 :(

@joaquinvaz
Copy link

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

@fxamauri
Copy link

fxamauri commented Jan 5, 2024

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

@UgurGumushan
Copy link

UgurGumushan commented May 23, 2024

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