Feat/event driven wait for push id token #1859
Open
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
Add native event-driven wait mechanism for push subscription ID/token to eliminate race condition where values return null immediately after permission grant.
Details
Motivation
The
OneSignal.User.pushSubscription.getIdAsync()andgetTokenAsync()methods have a race condition where they returnnullimmediately after permission is granted, even though the subscription ID/token is being generated asynchronously by the native SDK. The SDK provides no built-in mechanism to wait for the ID/token, forcing developers to implement their own retry/polling workarounds.This PR implements a native event-driven wait mechanism at the native level (Android/iOS) that waits for subscription ID/token generation using event listeners, providing a reliable built-in solution that waits 10-100ms for the value to be available.
Scope
What changes:
getIdAsync()andgetTokenAsync()now accept an optionaltimeoutparameter (default: 5000ms)What doesn't change:
Implementation Details
Native Code
Android (
RNOneSignal.java:517-612):waitForPushSubscriptionIdAsync(timeoutMs, promise)[1]waitForPushSubscriptionTokenAsync(timeoutMs, promise)[1]IPushSubscriptionObserverto listen for subscription changesHandler.postDelayedView related changes in
RNOneSignal.java›iOS (
RCTOneSignalEventEmitter.m:505-576):waitForPushSubscriptionIdAsync[1]waitForPushSubscriptionTokenAsync[1]OSPushSubscriptionObserverto listen for subscription changesdispatch_after__blockstorage for proper cleanup and preventing duplicate resolutionsView related changes in
RCTOneSignalEventEmitter.m›JavaScript/TypeScript API
TypeScript (
src/index.ts:316-395):Both methods enhanced with optional configuration:
Default timeout: 5000ms (5 seconds)
Returns: Subscription ID/token, or null if not available after timeout
View related changes in
src/index.ts›Architecture
Before (No wait mechanism):
After (Event-driven wait):
Example Usage
Key Features
Testing
Unit testing
Added comprehensive test coverage for both
getIdAsync()andgetTokenAsync():For
getIdAsync():For
getTokenAsync():All tests pass with 95%+ coverage threshold maintained.
Manual testing
Testing can be performed with:
Recommended manual testing scenario:
getIdAsync()after permission grantAffected code checklist
Checklist
Overview
timeoutparameter togetIdAsync()andgetTokenAsync()Testing
Final pass
Breaking Changes
None - This is fully backward compatible. The original methods had no parameters, and the new optional
timeoutparameter has a sensible default (5000ms), so all existing code continues to work without any changes.This change is