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 d4354e79..89710b1b 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -113,16 +113,20 @@ describe('OneSignal', () => { expect(mockRNOneSignal.initialize).not.toHaveBeenCalled(); }); - test('should not initialize if appId is null', () => { + test('should forward a null appId to native', () => { 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', () => { 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 is required'); + expect(errorSpy).toHaveBeenCalledWith('[OneSignal] initialize: appId must be a string'); }); test('should keep a permission event that arrives before the startup read resolves', async () => { @@ -194,16 +198,20 @@ describe('OneSignal', () => { expect(mockRNOneSignal.login).not.toHaveBeenCalled(); }); - test('should not login if externalId is null', () => { + test('should forward a null externalId to native', () => { 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', () => { 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 is required'); + expect(errorSpy).toHaveBeenCalledWith('[OneSignal] login: externalId must be a string'); }); }); diff --git a/src/index.ts b/src/index.ts index c057a2ec..26f834d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ import { isNativeModuleLoaded, isFunction, isMissing, + isNotStringOrNullish, isObject, isObjectSerializable, isValidCallback, @@ -137,7 +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 (isMissing(appId, 'initialize: appId')) return; + if (isNotStringOrNullish(appId, 'initialize: appId')) return; RNOneSignal.initialize(appId); @@ -152,7 +153,7 @@ export namespace OneSignal { */ export function login(externalId: string) { if (!isNativeModuleLoaded(RNOneSignal)) return; - if (isMissing(externalId, 'login: externalId')) return; + if (isNotStringOrNullish(externalId, 'login: externalId')) return; RNOneSignal.login(externalId); }