Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion ios/RCTOneSignal/RCTOneSignalEventEmitter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
11 changes: 11 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
28 changes: 18 additions & 10 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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');
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isNativeModuleLoaded,
isFunction,
isMissing,
isNotStringOrNullish,
isObject,
isObjectSerializable,
isValidCallback,
Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: null is now an intended input here, but the Spec (NativeOneSignal.ts:8) still types appId: string, and the iOS bridge (RCTOneSignalEventEmitter.mm:136) still marks it _Nonnull. It works at runtime since TurboModules pass nil through, but string | null and _Nullable would match the behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. string | null on the Spec and _Nullable on the bridge would match the intent. Not done yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Half done in 217ae1c: the iOS bridge parameter is _Nullable now.

Left the Spec as appId: string. Changing it means codegen plus a native build to verify, and CI here is JS-only, so I would be pushing an unverified codegen change. Happy to do it if you want it in this PR.

if (!isNativeModuleLoaded(RNOneSignal)) return;
if (isMissing(appId, 'initialize: appId')) return;
if (isNotStringOrNullish(appId, 'initialize: appId')) return;

RNOneSignal.initialize(appId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Blocking: on Android, RNOneSignal.java:243 sets oneSignalInitDone = true no matter what. On a fresh install with no cached app id, initialize(null) fails init in the SDK, but the bridge still latches. A later initialize(realId) in the same process then hits the early return at :229 and gets dropped until restart. Before this PR the JS guard kept that from happening.

Could we only latch when appId is non-empty, or just let the SDK's own already-initialized check handle repeat calls? initWithContext's return value won't work here, since it returns true once init is queued, before the app id is resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and you are right that initWithContext's return value will not help. Not fixed.

It sits behind a bigger blocker I had wrong: this needs SDK_NEVER_CRASH_ENABLED rolled out, not just the oneSignalVersion bump. Legacy OneSignalImp still throws on null by design.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now fixed in 217ae1c. oneSignalInitDone latches only once an app id is present, so a later initialize with a real one is no longer swallowed. Left your point about the return value in the comment, since it explains why the obvious fix does not work.


Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: when native ignores init (null appId and nothing cached), the startup reads below still run, so the user sees "failed to read initial state" instead of the real problem.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Noted. The startup read failure would mask the real cause.

Expand All @@ -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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Non-blocking: removing isMissing also removes the string type check, for both initialize and login. If untyped JS passes a number, Android's JavaTurboModule now throws a conversion exception instead of us logging and returning. Maybe keep a small guard like if (value != null && typeof value !== 'string') so null and "" still reach native.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch that isMissing was doing double duty here. If a guard stays it should be type-only, so null and "" still reach native. Not done yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now fixed in 217ae1c. Added isNotStringOrNullish, which rejects a non-string but lets null, undefined and "" through for native to decide on. Covered by two tests.

}
Expand Down
Loading