Skip to content

Conversation

@abdulraqeeb33
Copy link
Contributor

@abdulraqeeb33 abdulraqeeb33 commented Oct 29, 2025

Description

One Line Summary

Move slow/IO-bound parts of initWithContext off the main thread and add an async overload with a completion callback for internal usage. Added new suspend methods.

Motivation

Avoid main-thread stalls and potential ANRs during initialization, especially when SharedPreferences or other disk/network operations are involved. Provide an async path so app components (Activities/Receivers) can trigger initialization without blocking UI or risking Receiver timeouts.

Scope

  • initWithContext(context, appId): now move all initialization to background and return immediately.
  • initWithContext(context) is now suspend and can only be called from a coroutine. This method should used by internal classes only. It will remain suspend until all initialization is completed.
  • Accessors remain blocked if called immediately after initWithContext and before initialization is fully completed. This is to ensure no breaking change or expectation on existing consumer use cases.
  • Receivers will now call initWithContext(contex) in background, and switch back to main on tasks that must be executed in main thread, eg, finish(), startActivity() and other app lifecycle or UI operations.
  • Workers run doWork() in background will now directly call initWithContext(contex)
  • Retain the existing synchronous overload for backward compatibility; the new overload provides a non-blocking path with a completion callback.
  • Introduces a centralized ANR timeout constant.
  • No behavioral change to notification delivery formats, APIs, or user-visible features.

OPTIONAL - Other

OPTIONAL - Feel free to add any other sections or sub-sections that can explain your PR better.

Testing

Unit testing

  • New Robolectric/Kotest tests cover initWithContext in different scenarios. Ex:
    • Accessors throw before initialization.
    • Init does not block even when SharedPreferences access is artificially delayed.
    • Tags, local onesignalId, push subscription, and immediate login right after init.
  • LatchAwaiterTests testing the genetic functionalities of the waiter.

Manual testing

  • Cold-start the sample app with logging at VERBOSE. Ensure all initial operations are created in the same order.
  • Trigger init from an Activity and verify UI remains responsive.
  • For Receivers: simulate a boot or FCM broadcast; verify the receiver returns promptly (no >10s wait), and OneSignal completes initialization shortly after.
  • Notification click when app is killed / backgrounded.
  • Login, logout, addTag, addTrigger, permission update, IAM interaction
  • Environment: Android Studio Hedgehog or later; Pixel 6 emulator API 33; fresh install of the demo app.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

jinliu9508 and others added 13 commits September 26, 2025 17:01
Refactor: move part of initWithContext off main thread
…2374)

* feat: Add Kotlin MainApplication and suspend initialization support

- Add MainApplicationKT.kt as Kotlin version of MainApplication.java
- Add initWithContextSuspend() method for async initialization
- Refactor OneSignalImp to use IO dispatcher internally for initialization
- Add comprehensive unit tests for suspend initialization
- Rename LatchAwaiter to CompletionAwaiter for better semantics
- Add helper classes for user management (AppIdHelper, LoginHelper, LogoutHelper, UserSwitcher)
- Update build.gradle to include Kotlin coroutines dependency
- Ensure ANR prevention by using background threads for initialization

* Added more tests

* mandating passing app id in the login/logout methods

* linting

* Made app id mandatory for login and logout.

* cleanup

* reduce the forks

* Time out, deprecate annotation and appid,context

* ktlin

* include MainApplication.java, locks, early returns

* chore: Dispatcher Threads (#2375)

* Using dispatcher

* Update threads to 2

* Updated methods

* linting

* readme

* using the same thread pool

* lint

* making sure initstate has the right value

* lint

* Clear state and skip performance tests

* lint

* clear preferences

* fixing tests

* fixing tests

* fixing tests

* fixing tests

* fixing tests

* addressed PR comments

* Addressed comments and fixed tests

* lint

* lint

* fix test

* lint

* rewrote the test

* fix test

* made the test more robust

* clear all preferences and simplified mocks

* added more robustness

---------

Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>

* Fix OperationRepoTests CI/CD flakiness by using individual coVerify calls

- Replace coVerifyOrder with individual coVerify(exactly = 1) calls
- Makes tests more resilient to timing variations in CI/CD environments
- Maintains verification of all critical operations while allowing flexibility in exact timing

* remove try catch and lint

* Addressed comments, removed global scope launches, broke down userswitcher

* fixed flag

* fix tests

* making sure using the name instead of value

* test isolation

* logout test

* comments

* cleanup

---------

Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* broke down permissions activity

* fixed the permissions dialog

* cleanup

* permissions tests

* lint

* waiting to false for pause

* added some tests for pause

* uncommented the code

* removed duplicate class

* fixed wrong import

* fixed imports

* fixed bug to display the dialog after cancelling

* formatting ktlint

* tests

* lint

* adding delay

* removing the theme and going back to production

* add a delay

* add a delay

---------

Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@abdulraqeeb33 abdulraqeeb33 merged commit 6fa661d into main Oct 29, 2025
2 checks passed
@abdulraqeeb33 abdulraqeeb33 deleted the 5.4-main branch October 29, 2025 16:42
@github-actions github-actions bot mentioned this pull request Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants