-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat] Application service focus update, cold start creates new session and drives user refresh #2113
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nan-li
changed the title
[Feat] Cold start create new session and drives user refresh
[Feat] Cold start creates new session and drives user refresh
Jun 6, 2024
23 tasks
nan-li
force-pushed
the
fix/cold_start_new_session
branch
6 times, most recently
from
June 7, 2024 18:18
5d9abc5
to
941e4a0
Compare
* There are multiple checks for the conditional `(!isInForeground || nextResumeIsFirstActivity)` used in the Application Service and it is not obvious what this compound condition denotes
* Typically, the app foregrounding will trigger the ApplicationService to fire `onFocus` to its subscribers. * However, it is possible for OneSignal to initialize too late, so the ApplicationService itself is started too late to capture the application's early lifecycle events. * In this case, the app is already foregrounded, so fire a subscriber's `onFocus` callback immediately upon it subscribing, as many services have logic to run in the first onFocus call of a cold start.
* The getter for the session model's validity property will default to `false` to drive the creation of a new session. This is commonly read on new installs.
* focusTime is a Unix timestamp, default it to now instead of 0
* The SessionService was previously relying on the app backgrounding for 30 seconds to create a new session. It was doing this with the completion of a delayed background runnable. * Now it will also create a new session on all cold starts. * Also fix a bug where a cold start even over 30 seconds was previously not detecting the start of a session correctly. On wrappers, it is common for OneSignal to initialize too late to capture the Android lifecycle callbacks, which was the primary way of session detection. Now, there is enhanced logic to detect this in the onFocus callback and trigger subscribers correctly. Reset session model on cold starts - On cold starts, the app may not be in the background for over 30 seconds, which is when the session time is sent and the session model's `isValid` property is set to false. - Therefore, after the session model is read from cache, reset it's `isValid` property to `false` in order to drive the creation of a new session on cold starts.
- Now that cold starts will trigger a new session, the logic for storing active duration is updated. - Instead of active duration being reset to 0 at the start of every new session, it will reset to 0 when the session time is sent to the server. This is because cold starts may have stopped the old session time from sending, so we should not overwrite by resetting to 0 in the start method. - Remove the isValid check in the backgroundRun method. This is because a force killed app will initialize OneSignal to run this background task. By initializing OneSignal, the SDK believes this could be a cold start and set the isValid property on the session model to `false`. However, we want to send off this accumulated session time to the server, and that check would return early. - Additionally, the only listener of the onSessionEnded callback is the SessionListener. It will return early when session time is invalid. We have a similar guard in player model.
* New sessions can happen when the app is backgrounded over 30 seconds. Let's refresh the user then, not just when the app is cold started. * When UserRefreshService starts up, it subscribes to the SessionService who will notify it when a new session is started either from backgrounding or from a cold start. * The user will still be refreshed only when the app is in the foreground.
…nStarted * The onSessionStarted callback already will fetch messages. * Now that session starting (including cold starts) should always trigger the subscribers, there is no need to also fetch messages within onFocus
* Update existing tests * Extract helper methods them into a helper class for reuse * SessionServiceTests - Add test "session created in start when application is in the foreground" * ApplicationServiceTests - Add test "focus will occur on subscribe when activity is already started"
nan-li
force-pushed
the
fix/cold_start_new_session
branch
from
June 10, 2024 22:12
941e4a0
to
5aa61f7
Compare
nan-li
changed the title
[Feat] Cold start creates new session and drives user refresh
[Feat] Application service focus update, cold start creates new session and drives user refresh
Jun 11, 2024
jkasten2
requested changes
Jun 12, 2024
...signal/core/src/main/java/com/onesignal/core/internal/application/impl/ApplicationService.kt
Outdated
Show resolved
Hide resolved
.../onesignal/core/src/main/java/com/onesignal/session/internal/session/impl/SessionListener.kt
Outdated
Show resolved
Hide resolved
* Still send likely erroneous session time. This way the server can be aware of any issues. * Simplify state when firing subscribers of the Application Service
nan-li
force-pushed
the
fix/cold_start_new_session
branch
from
June 12, 2024 21:54
32fec1b
to
9b582e0
Compare
jkasten2
approved these changes
Jun 12, 2024
This was referenced Jun 18, 2024
Merged
This was referenced Jun 21, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
(1) All cold starts create new sessions, (2) refresh user on all new sessions, (3) Application Service fills in gaps where it did not fire
onFocus
for awaiting subscribers.Details
❓ Is my interpretation of this commit ApplicationService: Clarify a condition by naming it accurate?
❓ Now that "refresh-user" and "track-session-start" always enqueue at the same time, the
UserRefreshService
could be removed and its task could move to theSessionListener
.Application Service will fire
onFocus
even when it is too lateonFocus
to its subscribers.onFocus
callback immediately upon it subscribing, as many services have logic to run in the firstonFocus
call of a cold start, and pass a parameter so the subscriber knows if the callback is fired from subscribing or from natural application lifecycle callbacks. Some subscribers such as permission controllers do not want the callback when it is fired from just subscribing.Session service creates new session on cold start
ApplicationService
tostart()
and capture the AndroidonActivityStarted
oronActivityResumed
lifecycle callbacks, which was the primary way of session detection.FocusTimeController
in theinit(context)
method when the ActivityLifecycleHandler is added AND the application is already in the foreground: link.Session service updates tracking active duration with cold starts
Refresh user on new sessions (not just cold start)
Motivation
Scope
New session creation and when users are refreshed
Application Service will always fire
onFocus
when services are started uponCreate
call to capture and fire the earlyonFocus
callbackTesting
Unit testing
Update Session Service and Application Service Tests
SessionServiceTests
- Add test"session created in start when application is in the foreground"
ApplicationServiceTests
- Add test"focus will occur on subscribe when activity is already started"
Manual testing
Tested these primary scenarios
onCreate
Checked for:
track-session-start
operation)refresh-user
operation )Affected code checklist
Checklist
Overview
Testing
Final pass
This change is