Repository navigation
fix: [SDK-5366] catch native init errors on .NET Android - #208
Open
abdulraqeeb33 wants to merge 4 commits into
Open
abdulraqeeb33 wants to merge 4 commits into
abdulraqeeb33 wants to merge 4 commits into
Conversation
Native Android throws IllegalStateException from login, logout, and the
User, Session, Notifications, InAppMessages, and Location getters when
init has not started. The wrapper passed that straight to the app, so a
call before Initialize, or after Initialize("") returned early, crashed.
Skip those native calls and log once per API until InitWithContext has
been attempted. A started init that fails still throws. Debug and the
consent setters stay ungated because native accepts them before init.
Log the missing appId through Android logcat so release builds show it.
Co-authored-by: Cursor <cursoragent@cursor.com>
abdulraqeeb33
marked this pull request as draft
October 8, 2026 18:25
Drop the wrapper init flag. Call native and catch IllegalStateException, logging native's own message at error level and returning the fallback. Native stays the only source of init state, so calls it would accept are never skipped. Co-authored-by: Cursor <cursoragent@cursor.com>
InitWithContext returns while native init is still running. Registering the user, notification, and in-app message observers waits for it, so a failed init (for example a locked device in direct boot) threw out of Initialize. Wrap each registration so one failure does not skip the rest. PermissionNative now reports NotDetermined when native cannot answer, instead of Denied. Co-authored-by: Cursor <cursoragent@cursor.com>
InputGuard lives in the shared library, which cannot reference Android logging, and its Debug.WriteLine calls were compiled out because the Core debug build defined no DEBUG constant. Keep the console line and let Android also write the same message to logcat. Initialize uses the guard again. Co-authored-by: Cursor <cursoragent@cursor.com>
abdulraqeeb33
marked this pull request as ready for review
October 8, 2026 19:47
fadi-george
requested changes
Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
One Line Summary
Catch and log the
IllegalStateExceptionnative Android throws for calls made beforeInitialize, instead of letting it crash the app.Details
Motivation
Native Android throws
IllegalStateException("Must call 'initWithContext' before ...") fromlogin,logout, and theUser,Session,Notifications,InAppMessages, andLocationgetters when init has not started. The wrapper passed that to the app, soOneSignal.Loginand similar crashed unless the app caught it.SDK-5334 made this easier to hit:
Initialize("")returns beforeInitWithContext, so a later validLoginthrows. Native Android would have initialized with an empty app id. iOS already logs and returns before init.Fixes SDK-5366.
Scope
NativeCallhelper (Run/Get) wraps the native calls that go throughwaitForInit. It catches onlyJava.Lang.IllegalStateException, logs"<api> ignored: <native message>"at error level to logcat, and returnsfalse,null, or the given fallback. Any other exception still reaches the app.Login,Logout, and the user, push subscription, session, notifications, and in-app messages calls.Initializeis wrapped too, one call per manager.InitWithContextreturns while native init is still running, and registration waits for it, so a failed init (for example a locked device in direct boot) used to throw out ofInitialize.Debugand the consent setters (native accepts both before init).AndroidLocationManageris unchanged; it already catches.InputGuardrejection (empty app id, empty login, missing tag key, and the rest) writes the debug console line and, on Android, the same message to logcat.Initializeuses the guard again.Behavior change
A failed init after a real
Initialize("appId")no longer throws from later calls. It shows up as a logcat error carrying native's message (OneSignal initWithContext failed.). This matches iOS, which logs and returns.Known gaps
Notifications.Permission,PushSubscription.OptedIn, andRequestPermissionAsyncreturnfalse, which a caller cannot tell from a real denial.PermissionNative()returnsNotDetermined.PushSubscription.OptOut()andInAppMessages.Paused = trueare the risky ones: the app's intent is lost and only logcat shows it.IllegalStateExceptionnative throws for a reason other than init is also logged and dropped.AndroidLocationManagercatchesException(existing behavior), so an init error there is logged as a missing location module.Testing
Unit testing
None. This repo has no .NET test project.
Manual testing
dotnet buildofOneSignalSDK.DotNet.Androidpasses with no warnings. Not exercised on a device yet.Affected code checklist
Checklist
Overview
Testing
Final pass