Skip to content

Conversation

@nan-li
Copy link
Contributor

@nan-li nan-li commented Dec 5, 2025

Description

One Line Summary

Fix reported crashes when handlers / observers are removed on destroy and the SDK is uninitialized or cleaned up already.

Details

To address #1554

  • When the activity is destroyed, the code tries to remove handlers by calling OneSignal.getInAppMessages(), but the native OneSignal SDK may not be fully initialized yet (or may have been cleaned up), causing it to throw "Must call 'initWithContext' before use".
  • Also move the oneSignalInitDone flag further down past the native OneSignal.initWithContext(context, appId) call. The native SDK already handles if initWithContext is called while it is still initializing.

Motivation

Bug fix, fix crash

Scope

When activity being destroyed

Testing

Manual testing

Manually ran app in Android 35 emulator, app initializes, gets IAMs, get notifications.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

* Wrap removeHandlers() and removeObservers() calls in try-catch blocks within onHostDestroy() and onCatalystInstanceDestroy() to handle cases where the native OneSignal SDK throws "Must call 'initWithContext' before use" during activity destruction.
* Also move the oneSignalInitDone flag further down past the native `OneSignal.initWithContext(context, appId)` call.
@nan-li nan-li requested a review from a team as a code owner December 5, 2025 02:33
@nan-li nan-li requested review from a team and removed request for a team December 5, 2025 02:36
@fadi-george
Copy link
Collaborator

Change seem reasonable. I would consider updating/adding test for these.

Copy link

@abdulraqeeb33 abdulraqeeb33 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also can we directly check the init from the OneSignal class?

removeHandlers();
removeObservers();
} catch (Exception e) {
Logging.debug("OneSignal SDK not fully initialized. Could not remove handlers/observers: " + e.getMessage(), null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception seems like a catch all.

I think we should first check if the SDK is initalized or not and then catch.

if(!oneSignalInitDone) {
Logging.debug("OneSignal React-Native SDK not initialized yet. Could not remove observers.", null);
return;
}
try {
            removeHandlers();
            removeObservers();
} catch (Exception e) {
            Logging.debug("Could not remove handlers/observers: " + e.getMessage(), null);
} 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the init done flag further down so that the SDK is done initializing. This hopefully is enough to prevent the crash without a try catch, which may be excessive after the init done flag change.

@Override
public void onCatalystInstanceDestroy() {
removeHandlers();
removeObservers();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here.

};

private void removeObservers() {
if(!oneSignalInitDone) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also can we try a test for this?

@nan-li nan-li requested a review from abdulraqeeb33 December 9, 2025 16:59
@nan-li
Copy link
Contributor Author

nan-li commented Dec 9, 2025

Change seem reasonable. I would consider updating/adding test for these.

@fadi-george When you worked on tests, did you get native code to be tested?

@fadi-george
Copy link
Collaborator

Change seem reasonable. I would consider updating/adding test for these.

@fadi-george When you worked on tests, did you get native code to be tested?

I only added typescript tests

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

Successfully merging this pull request may close these issues.

4 participants