feature: Exposing accessors thru suspend
Description
One Line Summary
Expose suspend-safe accessor methods for all SDK managers and configuration properties to prevent ANRs and improve thread safety.
Details
This PR adds suspend-based accessor methods to the OneSignal class, providing non-blocking alternatives to the existing property accessors. These methods are designed to work seamlessly with Kotlin coroutines and prevent Application Not Responding (ANR) errors by ensuring all SDK operations run on background threads.
New Suspend Accessor Methods
Manager Accessors:
getUserSuspend()- Suspend-safe version ofUserpropertygetSessionSuspend()- Suspend-safe version ofSessionpropertygetNotificationsSuspend()- Suspend-safe version ofNotificationspropertygetLocationSuspend()- Suspend-safe version ofLocationpropertygetInAppMessagesSuspend()- Suspend-safe version ofInAppMessagesproperty
Configuration Property Accessors:
getConsentRequiredSuspend()/setConsentRequiredSuspend()- Suspend-safe version ofconsentRequiredpropertygetConsentGivenSuspend()/setConsentGivenSuspend()- Suspend-safe version ofconsentGivenpropertygetDisableGMSMissingPromptSuspend()/setDisableGMSMissingPromptSuspend()- Suspend-safe version ofdisableGMSMissingPromptproperty
User Management Methods:
loginSuspend()- Suspend-safe version oflogin()method (already existed, improved documentation)logoutSuspend()- Suspend-safe version oflogout()method (already existed, improved documentation)
Key Benefits
- ANR Prevention: All suspend methods automatically handle SDK initialization waiting on background threads, preventing main thread blocking
- Thread Safety: Suspend methods ensure proper thread context switching using
Dispatchers.IO - Backward Compatible: Existing property accessors and methods remain unchanged - this is purely additive
- Consistent API: All suspend accessors follow the same naming pattern (
*Suspend) and behavior
Usage Example
in // Before (can cause ANR if called on main thread before init completes) val user = OneSignal.User OneSignal.consentRequired = true
// After (safe to call from any thread, including main thread) lifecycleScope.launch { val user = OneSignal.getUserSuspend() OneSignal.setConsentRequiredSuspend(true) }### Motivation
Starting in 5.4.0, we've been moving SDK operations to background threads to reduce ANR reports. While the existing property accessors work, they can still block threads if the SDK isn't fully initialized. These suspend methods provide a cleaner, more explicit way for developers to use the SDK in coroutine-based code without worrying about thread blocking or ANRs.
The suspend methods automatically:
- Wait for SDK initialization to complete (if in progress)
- Execute on the appropriate background thread (
Dispatchers.IO) - Return the same instances as their non-suspend counterparts
- Maintain full compatibility with existing code
Scope
- Added 11 new suspend accessor methods to
OneSignalclass - All methods are properly documented with KDoc comments
- Methods delegate to existing
IOneSignalinterface methods (no duplicate implementation) - All methods include
@JvmStaticannotation for Java interop - Comprehensive test coverage added to verify all methods exist and work correctly
Testing
Unit testing
Added comprehensive test suite (OneSignalSuspendAccessorsTests.kt) that verifies:
- All suspend accessor methods exist and return correct types
- All non-suspend accessor properties exist and work correctly
- Suspend and non-suspend accessors return the same instances
- Configuration properties work correctly in both suspend and non-suspend forms
- Login/logout suspend methods work correctly
All existing tests continue to pass.
Manual testing
Tested on:
- Low-end emulators (Android 8.0, 2GB RAM)
- High-end emulators (Android 14, 8GB RAM)
- Physical devices (Pixel 9a, various Android versions)
Verified that:
- Suspend methods work correctly from coroutines
- No ANRs occur when using suspend methods from main thread
- All methods return expected types and values
- Suspend and non-suspend accessors are consistent
Affected code checklist
- [x] Notifications
- [x] Display (via
getNotificationsSuspend()) - [x] Open (via
getNotificationsSuspend()) - [x] Push Processing (via
getNotificationsSuspend()) - [x] Confirm Deliveries (via
getNotificationsSuspend())
- [x] Display (via
- [x] Outcomes (via
getUserSuspend()) - [x] Sessions (via
getSessionSuspend()) - [x] In-App Messaging (via
getInAppMessagesSuspend()) - [x] REST API requests (all suspend methods use background threads)
- [x] Public API changes
Checklist
Overview
- [x] I have filled out all REQUIRED sections above
- [x] PR does one thing - exposes suspend accessor methods for better ANR prevention
- [x] Any Public API changes are explained in the PR details and conform to existing APIs
Testing
- [x] I have included test coverage for these changes, or explained why they are not needed
- [x] All automated tests pass, or I explained why that is not possible
- [x] I have personally tested this on my device, or explained why that is not possible
Final pass
- [x] Code is as readable as possible
- All methods follow consistent naming pattern
- Comprehensive KDoc comments explain usage and behavior
- Clear separation between suspend and non-suspend APIs
- [x] I have reviewed this PR myself, ensuring it meets each checklist item