From ce153d49eaaacb0de63c2b9fd14e77fcfdaa5e9a Mon Sep 17 00:00:00 2001 From: AR Abdul Azeez Date: Fri, 20 Nov 2026 10:12:08 -0600 Subject: [PATCH 1/2] fix: [SDK-5698] let initialize and login reach native Both guards returned before calling native. Initialize was the damaging one: native falls back to the cached app id, and short-circuiting skipped that fallback, leaving the SDK unstarted so the next login failed. Safe on both platforms, checked rather than assumed: Android initWithContext and login now take String? and log and ignore a null, after the never-crash change. iOS initialize: explicitly handles nil and falls back to the cached app id. The RN bridge's login already rejects a nil or empty externalId itself, in RCTOneSignalEventEmitter.mm, so native iOS login never sees one. isNativeModuleLoaded stays. So do the alias, email, SMS, tag, trigger, and outcome guards: those native parameters are still non-null. The four tests that asserted "do not call native" now assert the call is forwarded. Reverting either guard turns them red, which I confirmed. DO NOT MERGE until oneSignalVersion in android/build.gradle can be bumped to a release containing the never-crash change. It pins 5.11.0, where initWithContext and login take non-null Kotlin parameters. Co-authored-by: Cursor --- src/index.test.ts | 20 ++++++++------------ src/index.ts | 2 -- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index d4354e79..2fa4c2d8 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -113,16 +113,14 @@ describe('OneSignal', () => { expect(mockRNOneSignal.initialize).not.toHaveBeenCalled(); }); - test('should not initialize if appId is null', () => { + test('should forward a null appId to native, which falls back to the cached app id', () => { OneSignal.initialize(null as unknown as string); - expect(mockRNOneSignal.initialize).not.toHaveBeenCalled(); - expect(errorSpy).toHaveBeenCalledWith('[OneSignal] initialize: appId is required'); + expect(mockRNOneSignal.initialize).toHaveBeenCalledWith(null); }); - test('should not initialize if appId is empty', () => { + test('should forward an empty appId to native, which falls back to the cached app id', () => { OneSignal.initialize(''); - expect(mockRNOneSignal.initialize).not.toHaveBeenCalled(); - expect(errorSpy).toHaveBeenCalledWith('[OneSignal] initialize: appId is required'); + expect(mockRNOneSignal.initialize).toHaveBeenCalledWith(''); }); test('should keep a permission event that arrives before the startup read resolves', async () => { @@ -194,16 +192,14 @@ describe('OneSignal', () => { expect(mockRNOneSignal.login).not.toHaveBeenCalled(); }); - test('should not login if externalId is null', () => { + test('should forward a null externalId to native, which logs and ignores it', () => { OneSignal.login(null as unknown as string); - expect(mockRNOneSignal.login).not.toHaveBeenCalled(); - expect(errorSpy).toHaveBeenCalledWith('[OneSignal] login: externalId is required'); + expect(mockRNOneSignal.login).toHaveBeenCalledWith(null); }); - test('should not login if externalId is empty', () => { + test('should forward an empty externalId to native, which logs and ignores it', () => { OneSignal.login(''); - expect(mockRNOneSignal.login).not.toHaveBeenCalled(); - expect(errorSpy).toHaveBeenCalledWith('[OneSignal] login: externalId is required'); + expect(mockRNOneSignal.login).toHaveBeenCalledWith(''); }); }); diff --git a/src/index.ts b/src/index.ts index c057a2ec..2cdf78d1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -137,7 +137,6 @@ export namespace OneSignal { /** Initializes the OneSignal SDK. This should be called during startup of the application. */ export function initialize(appId: string) { if (!isNativeModuleLoaded(RNOneSignal)) return; - if (isMissing(appId, 'initialize: appId')) return; RNOneSignal.initialize(appId); @@ -152,7 +151,6 @@ export namespace OneSignal { */ export function login(externalId: string) { if (!isNativeModuleLoaded(RNOneSignal)) return; - if (isMissing(externalId, 'login: externalId')) return; RNOneSignal.login(externalId); } From 217ae1c44a7bd4aacf9478da7e9725e84e0710c4 Mon Sep 17 00:00:00 2001 From: AR Abdul Azeez Date: Fri, 20 Nov 2026 15:40:31 -0600 Subject: [PATCH 2/2] fix: [SDK-5698] stop a missing app id from latching Android init RNOneSignal.java set oneSignalInitDone regardless of outcome. With no app id and nothing cached, init fails but the latch still happens, so a later initialize with a real app id hit the already-initialized early return for the rest of the process. It now latches only once an app id is present. initWithContext's return value cannot stand in: it reports that init was queued, before the app id is resolved. Removing isMissing also removed the string type check it was doing double duty for, so an untyped caller passing a number would hit a TurboModule conversion error. Added isNotStringOrNullish, which rejects a non-string while letting null, undefined and "" through for native to decide on. Marked the iOS initialize bridge parameter _Nullable, since nil is now an intended input there. Two test titles claimed behavior they did not check, and are narrowed to what they assert. Left out: typing appId as string | null in the TurboModule Spec. It needs codegen plus a native build to verify, and CI here is JS-only. Review feedback from fadi-george on #2017. Co-authored-by: Cursor --- .../rnonesignalandroid/RNOneSignal.java | 9 ++++++++- ios/RCTOneSignal/RCTOneSignalEventEmitter.mm | 4 +++- src/helpers.ts | 11 ++++++++++ src/index.test.ts | 20 +++++++++++++++---- src/index.ts | 3 +++ 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java index 4a964420..7b94bafc 100644 --- a/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java +++ b/android/src/main/java/com/onesignal/rnonesignalandroid/RNOneSignal.java @@ -240,7 +240,14 @@ public void initialize(String appId) { Logging.debug( "OneSignal initialize using context: " + context.getClass().getSimpleName(), null); OneSignal.initWithContext(context, appId); - oneSignalInitDone = true; + + // Latch only once an app id is present. Without one, and with nothing cached, init fails, + // so latching would turn a later initialize with a real app id into the no-op branch above + // for the rest of the process. initWithContext's return value cannot stand in here: it + // reports that init was queued, before the app id has been resolved. + if (appId != null && !appId.isEmpty()) { + oneSignalInitDone = true; + } } @Override diff --git a/ios/RCTOneSignal/RCTOneSignalEventEmitter.mm b/ios/RCTOneSignal/RCTOneSignalEventEmitter.mm index 180c35eb..ae88b140 100644 --- a/ios/RCTOneSignal/RCTOneSignalEventEmitter.mm +++ b/ios/RCTOneSignal/RCTOneSignalEventEmitter.mm @@ -133,7 +133,9 @@ + (void)sendEventWithName:(NSString *)name withBody:(NSDictionary *)body { #pragma mark Exported Methods // OneSignal root namespace methods -RCT_EXPORT_METHOD(initialize : (NSString *_Nonnull)appId) { +// Nullable on purpose: native reads nil as "no app id supplied" and falls back to the one cached +// by an earlier launch, so JS is allowed to pass null through. +RCT_EXPORT_METHOD(initialize : (NSString *_Nullable)appId) { [OneSignal initialize:appId withLaunchOptions:NULL]; } diff --git a/src/helpers.ts b/src/helpers.ts index d51f360e..5a8badc5 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -24,6 +24,17 @@ export function isMissing(value: unknown, api: string): boolean { return true; } +/** + * Rejects a value native cannot marshal as a string, while letting null, undefined and "" through + * for native to decide on. Use where native owns the empty and missing cases but the TurboModule + * would still throw a conversion error on, say, a number. + */ +export function isNotStringOrNullish(value: unknown, api: string): boolean { + if (value == null || typeof value === 'string') return false; + console.error(`[OneSignal] ${api} must be a string`); + return true; +} + export function isBoolean(value: unknown, api: string): value is boolean { if (typeof value === 'boolean') return true; console.error(`[OneSignal] ${api} must be a boolean`); diff --git a/src/index.test.ts b/src/index.test.ts index 2fa4c2d8..89710b1b 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -113,16 +113,22 @@ describe('OneSignal', () => { expect(mockRNOneSignal.initialize).not.toHaveBeenCalled(); }); - test('should forward a null appId to native, which falls back to the cached app id', () => { + test('should forward a null appId to native', () => { OneSignal.initialize(null as unknown as string); expect(mockRNOneSignal.initialize).toHaveBeenCalledWith(null); }); - test('should forward an empty appId to native, which falls back to the cached app id', () => { + test('should forward an empty appId to native', () => { OneSignal.initialize(''); expect(mockRNOneSignal.initialize).toHaveBeenCalledWith(''); }); + test('should not forward a non-string appId to native', () => { + OneSignal.initialize(42 as unknown as string); + expect(mockRNOneSignal.initialize).not.toHaveBeenCalled(); + expect(errorSpy).toHaveBeenCalledWith('[OneSignal] initialize: appId must be a string'); + }); + test('should keep a permission event that arrives before the startup read resolves', async () => { let resolveStartupRead: ((granted: boolean) => void) | undefined; vi.mocked(mockRNOneSignal.hasNotificationPermission).mockReturnValueOnce( @@ -192,15 +198,21 @@ describe('OneSignal', () => { expect(mockRNOneSignal.login).not.toHaveBeenCalled(); }); - test('should forward a null externalId to native, which logs and ignores it', () => { + test('should forward a null externalId to native', () => { OneSignal.login(null as unknown as string); expect(mockRNOneSignal.login).toHaveBeenCalledWith(null); }); - test('should forward an empty externalId to native, which logs and ignores it', () => { + test('should forward an empty externalId to native', () => { OneSignal.login(''); expect(mockRNOneSignal.login).toHaveBeenCalledWith(''); }); + + test('should not forward a non-string externalId to native', () => { + OneSignal.login(42 as unknown as string); + expect(mockRNOneSignal.login).not.toHaveBeenCalled(); + expect(errorSpy).toHaveBeenCalledWith('[OneSignal] login: externalId must be a string'); + }); }); describe('logout', () => { diff --git a/src/index.ts b/src/index.ts index 2cdf78d1..26f834d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ import { isNativeModuleLoaded, isFunction, isMissing, + isNotStringOrNullish, isObject, isObjectSerializable, isValidCallback, @@ -137,6 +138,7 @@ export namespace OneSignal { /** Initializes the OneSignal SDK. This should be called during startup of the application. */ export function initialize(appId: string) { if (!isNativeModuleLoaded(RNOneSignal)) return; + if (isNotStringOrNullish(appId, 'initialize: appId')) return; RNOneSignal.initialize(appId); @@ -151,6 +153,7 @@ export namespace OneSignal { */ export function login(externalId: string) { if (!isNativeModuleLoaded(RNOneSignal)) return; + if (isNotStringOrNullish(externalId, 'login: externalId')) return; RNOneSignal.login(externalId); }