feature: Exposing accessors thru suspend #2502
Open
+726
−7
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
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
OneSignalclass, 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 ofInAppMessagespropertyConfiguration Property Accessors:
getConsentRequiredSuspend()/setConsentRequiredSuspend()- Suspend-safe version ofconsentRequiredpropertygetConsentGivenSuspend()/setConsentGivenSuspend()- Suspend-safe version ofconsentGivenpropertygetDisableGMSMissingPromptSuspend()/setDisableGMSMissingPromptSuspend()- Suspend-safe version ofdisableGMSMissingPromptpropertyUser Management Methods:
loginSuspend()- Suspend-safe version oflogin()method (already existed, improved documentation)logoutSuspend()- Suspend-safe version oflogout()method (already existed, improved documentation)Key Benefits
Dispatchers.IO*Suspend) and behaviorUsage 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:
Dispatchers.IO)Scope
OneSignalclassIOneSignalinterface methods (no duplicate implementation)@JvmStaticannotation for Java interopTesting
Unit testing
Added comprehensive test suite (
OneSignalSuspendAccessorsTests.kt) that verifies:All existing tests continue to pass.
Manual testing
Tested on:
Verified that:
Affected code checklist
getNotificationsSuspend())getNotificationsSuspend())getNotificationsSuspend())getNotificationsSuspend())getUserSuspend())getSessionSuspend())getInAppMessagesSuspend())Checklist
Overview
Testing
Final pass
This change is