Skip to content

Commit fafbe73

Browse files
authored
SDK-33 inAppDisplayDelegate not displaying in-apps as expected (#1013)
1 parent 2a55d08 commit fafbe73

2 files changed

Lines changed: 63 additions & 9 deletions

File tree

swift-sdk/Internal/in-app/InAppManager.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
233233
if waitTime <= 0 {
234234
_ = scheduleSync()
235235
} else {
236-
ITBInfo("can't sync now, need to wait: \(waitTime)")
236+
ITBInfo("skipping sync (cooldown: \(waitTime)s), processing existing messages")
237+
238+
processExistingMessages()
237239
}
238240
}
239241

@@ -369,6 +371,15 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
369371
}
370372
}
371373

374+
private func processExistingMessages() {
375+
_ = InAppManager.getAppIsReady(applicationStateProvider: applicationStateProvider, displayer: displayer).map { [weak self] appIsActive in
376+
if appIsActive, let messagesMap = self?.messagesMap {
377+
self?.processAndShowMessage(messagesMap: messagesMap)
378+
self?.persister.persist(messagesMap.values)
379+
}
380+
}
381+
}
382+
372383
// This method schedules next triggered message after showing a message
373384
private func scheduleNextInAppMessage() {
374385
ITBDebug()
@@ -381,14 +392,7 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
381392
self?.scheduleNextInAppMessage()
382393
}
383394
} else {
384-
_ = InAppManager.getAppIsReady(applicationStateProvider: applicationStateProvider, displayer: displayer).map { [weak self] appIsActive in
385-
if appIsActive {
386-
if let messagesMap = self?.messagesMap {
387-
self?.processAndShowMessage(messagesMap: messagesMap)
388-
self?.persister.persist(messagesMap.values)
389-
}
390-
}
391-
}
395+
processExistingMessages()
392396
}
393397
}
394398

tests/unit-tests/InAppTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,56 @@ class InAppTests: XCTestCase {
777777
wait(for: [expectation4], timeout: testExpectationTimeout)
778778
}
779779

780+
func testShowBackgroundSyncedMessageOnForegroundWithinCooldown() {
781+
let expectation1 = expectation(description: "initial sync completes")
782+
let expectation2 = expectation(description: "background sync completes")
783+
let expectation3 = expectation(description: "message shown on foreground within cooldown")
784+
785+
let payload = TestInAppPayloadGenerator.createPayloadWithUrl(numMessages: 1)
786+
787+
let mockInAppFetcher = MockInAppFetcher()
788+
let mockDateProvider = MockDateProvider()
789+
790+
let mockInAppDisplayer = MockInAppDisplayer()
791+
mockInAppDisplayer.onShow.onSuccess { _ in
792+
expectation3.fulfill()
793+
}
794+
795+
let config = IterableConfig()
796+
config.inAppDisplayInterval = 1.0
797+
798+
let mockApplicationStateProvider = MockApplicationStateProvider(applicationState: .active)
799+
let mockNotificationCenter = MockNotificationCenter()
800+
801+
let internalApi = InternalIterableAPI.initializeForTesting(
802+
config: config,
803+
dateProvider: mockDateProvider,
804+
inAppFetcher: mockInAppFetcher,
805+
inAppDisplayer: mockInAppDisplayer,
806+
applicationStateProvider: mockApplicationStateProvider,
807+
notificationCenter: mockNotificationCenter
808+
)
809+
810+
// 1. Initial sync with no messages (sets lastSyncTime)
811+
mockInAppFetcher.mockMessagesAvailableFromServer(internalApi: internalApi, messages: []).onSuccess { _ in
812+
expectation1.fulfill()
813+
}
814+
wait(for: [expectation1], timeout: testExpectationTimeout)
815+
816+
// 2. App goes to background, new message arrives via background sync
817+
mockApplicationStateProvider.applicationState = .background
818+
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalApi, payload).onSuccess { _ in
819+
expectation2.fulfill()
820+
}
821+
wait(for: [expectation2], timeout: testExpectationTimeout)
822+
823+
// 3. App returns to foreground within cooldown (no time advance)
824+
mockApplicationStateProvider.applicationState = .active
825+
mockNotificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil, userInfo: nil)
826+
827+
wait(for: [expectation3], timeout: testExpectationTimeout)
828+
}
829+
780830
func testDontShowNewlyArrivedMessageWithinRetryInterval() {
781831
let expectation1 = expectation(description: "show first message")
782832
let expectation2 = expectation(description: "don't show second message within interval")

0 commit comments

Comments
 (0)