Skip to content
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

Fixed opened push not getting called when app is terminated #145

Merged
merged 1 commit into from
Jan 22, 2024

Conversation

ajaysubra
Copy link
Contributor

@ajaysubra ajaysubra commented Jan 19, 2024

Description

We were handling pending requests but when an app is terminated and launched again from a push notification open it triggers the open event and this open event is added to the pending requests since the SDK is not yet initialized (when the app is relaunched after terminating it's initialized again). However, between the SDK going from initializing to initialized the requests that were added to pending requests were ignored which is what was causing the opened push events from not getting enqueued and eventually sent out.

The fix was to just use the pending requests from state and enqueue each of those instead of using the state that was taken from the disk.

Check List

  • Are you changing anything with the public API?
  • Have you tested this change on real device?
  • Are your changes backwards compatible with previous SDK Versions?
  • Have you added unit test coverage for your changes?
  • Have you verified that your changes are compatible with all the operating system version this SDK currently supports?

Manual Test Plan

Supporting Materials

@ajaysubra ajaysubra requested a review from a team as a code owner January 19, 2024 21:42
@@ -144,11 +144,8 @@ struct KlaviyoReducer: ReducerProtocol {
}
state.initalizationState = .initializing
state.apiKey = apiKey
// carry over pending events
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are now doing this before completing initialization since we know that in certain cases requests may be added state.pendingRequests after this action has completed.

initialState.queue += queuedRequests
// For any requests that get added between initilizing and initilized.
// Ex: when the app is invoked from a push notification after being killed from the app switcher.
let pendingRequests = state.pendingRequests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line that actually fixes the issue. When we don't have this, the requests that are added to state.pendingRequests after the initialization action were erased from state without enqueuing them.

@@ -441,4 +441,31 @@ class StateManagementTests: XCTestCase {
try $0.enqueueRequest(request: .init(apiKey: XCTUnwrap($0.apiKey), endpoint: .createEvent(.init(data: .init(event: event, anonymousId: XCTUnwrap($0.anonymousId))))))
}
}

func testEnqueueEventWhenInitilizingSendsEvent() async throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test to validate the solution works.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome test!

Copy link
Contributor

@evan-masseau evan-masseau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a sort of second hand understanding of the root cause, and this fix makes sense to me based on that understanding!

@ajaysubra ajaysubra merged commit 5f073b5 into master Jan 22, 2024
7 checks passed
@ajaysubra ajaysubra deleted the as/chnl-4732-opened-push-fix branch January 22, 2024 21:45
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.

3 participants