OneSignal-Android-SDK
OneSignal-Android-SDK copied to clipboard
[Bug]: Fatal Exception: java.lang.NullPointerException when calling `com.onesignal.OneSignal$OSNotificationOpenedHandler.notificationOpened()`
What happened?
I'm observing some crash reports in Firebase Crashlytics with the following stack trace:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'void com.onesignal.OneSignal$OSNotificationOpenedHandler.notificationOpened(com.onesignal.OSNotificationOpenedResult)' on a null object reference
at com.onesignal.OneSignal$23.run(OneSignal.java:2263)
at android.os.Handler.handleCallback(Handler.java:978)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loopOnce(Looper.java:238)
at android.os.Looper.loop(Looper.java:357)
at android.app.ActivityThread.main(ActivityThread.java:8089)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
Steps to reproduce?
I'm not sure on the steps. Just seeing the reports in Crashlytics.
What did you expect to happen?
Crashes should not happen
OneSignal Android SDK version
react-native-onesignal: 4.3.5
Android version
13, 12, 10
Specific Android models
No response
Relevant log output
No response
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
Hi there, @RustamG! Based on the stack trace, it looks like you are using the Notification Opened Handler in your app? How/when are you invoking this method? Could you please share your code? Thanks!
Hi @jennantilla,
Our React-Native app looks like what's described in official guide. We call setNotificationOpenedHandler in componentDidMount()
of the entry-point screen component. Unfortunately I cannot share the whole source code of the app. But the related code is this (skipped some unrelated stuff):
import React, { Component } from 'react';
/*
... skipped some imports
*/
class App extends Component {
state = {
subscription: null,
};
componentDidMount() {
const subscription = subscribe(this.handleAuthTokenChange);
const { loadApp, appStore, initNotifications } = this.props;
this.setState({ subscription });
loadApp();
initNotifications(appStore);
}
componentWillUnmount() {
const { subscription } = this.state;
if (subscription) {
unsubscribe(subscription);
}
}
handleAuthTokenChange = () => {
const { loadApp } = this.props;
loadApp();
};
render() {
const { app } = this.props;
if (app.loading) return <Loader fixed green color="#FFFFFF" />;
if (!!app.token && !app.suspended) return <Navigator.InnerApp />;
if (!!app.token && app.suspended) return <Navigator.SuspendedServiceApp />;
return <Navigator.SignInApp />;
}
initNotifications
is passed via several layers. Basically it contains this:
OneSignal.setAppId(ONE_SIGNAL_API_KEY);
OneSignal.setNotificationWillShowInForegroundHandler(notifReceivedEvent => {
let notif = notifReceivedEvent.getNotification();
notifReceivedEvent.complete(notif);
this.onReceived(notif, true);
});
OneSignal.setNotificationOpenedHandler(this.onOpened);
I see such crashes only on Android. So I thought the cause is in SDK for Android. Let me know if more information is needed. Thanks.
Hello @RustamG, apologies that this issue has gone stale. Much of our work since 2023 H2 has been on our latest major release where many improvements and enhancements are introduced. We will continue to investigate 4.x.x bugs, though we encourage you to update to 5.1.0 as that will be the most-supported release going forward. Please let us know if you have any questions!