Skip to content

Commit 341fa18

Browse files
authored
Merge pull request #964 from Iterable/SDK-171-BCIT-InAppSilent-With-Silent-Push
[SDK-171] - BCIT - Silent push update for InApp
2 parents 0b86b69 + ea97372 commit 341fa18

File tree

4 files changed

+101
-23
lines changed

4 files changed

+101
-23
lines changed

integration-tests/src/androidTest/java/com/iterable/integration/tests/BaseIntegrationTest.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,43 @@ abstract class BaseIntegrationTest {
239239
customActionHandlerCalled.get()
240240
}, timeoutSeconds)
241241
}
242+
243+
/**
244+
* Wait for InAppUpdate push notification to be processed and in-app messages to sync.
245+
* Returns true if messages were synced (either via push or manually), false if timeout.
246+
*/
247+
protected fun waitForInAppSyncViaPush(
248+
initialMessageCount: Int,
249+
pushTimeoutSeconds: Long = 10
250+
): Boolean {
251+
Log.d("BaseIntegrationTest", "Waiting for InAppUpdate push to trigger sync (timeout: ${pushTimeoutSeconds}s)...")
252+
253+
// Wait for either:
254+
// 1. Silent push was processed (indicates InAppUpdate push arrived)
255+
// 2. Message count increased (indicates sync happened)
256+
val pushProcessed = waitForCondition({
257+
testUtils.isSilentPushProcessed() ||
258+
IterableApi.getInstance().inAppManager.messages.count() > initialMessageCount
259+
}, pushTimeoutSeconds)
260+
261+
if (pushProcessed) {
262+
Log.d("BaseIntegrationTest", "InAppUpdate push processed or sync detected")
263+
// Give a bit more time for sync to complete if push was just processed
264+
Thread.sleep(2000)
265+
} else {
266+
Log.d("BaseIntegrationTest", "InAppUpdate push not received within timeout")
267+
}
268+
269+
// Check if messages were synced
270+
val currentMessageCount = IterableApi.getInstance().inAppManager.messages.count()
271+
val syncHappened = currentMessageCount > initialMessageCount
272+
273+
if (syncHappened) {
274+
Log.d("BaseIntegrationTest", "✅ In-app messages synced via push (message count: $currentMessageCount)")
275+
} else {
276+
Log.d("BaseIntegrationTest", "⚠️ In-app messages not synced yet (message count: $currentMessageCount)")
277+
}
278+
279+
return syncHappened
280+
}
242281
}

integration-tests/src/androidTest/java/com/iterable/integration/tests/InAppMessageIntegrationTest.kt

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ class InAppMessageIntegrationTest : BaseIntegrationTest() {
134134
IterableApi.getInstance().inAppManager.removeMessage(it)
135135
}
136136

137+
// Get initial message count before triggering campaign
138+
val initialMessageCount = IterableApi.getInstance().inAppManager.messages.count()
139+
Log.d(TAG, "📊 Initial message count: $initialMessageCount")
140+
141+
// Reset silent push tracking to detect InAppUpdate push
142+
testUtils.setSilentPushProcessed(false)
143+
137144
var campaignTriggered = false
138145
val latch = java.util.concurrent.CountDownLatch(1)
139146

@@ -167,20 +174,33 @@ class InAppMessageIntegrationTest : BaseIntegrationTest() {
167174
return
168175
}
169176

170-
Log.d(TAG, "✅ Campaign triggered successfully, proceeding with message sync...")
177+
Log.d(TAG, "✅ Campaign triggered successfully, waiting for push-triggered sync...")
171178

172-
// Step 4: Sync messages
173-
Log.d(TAG, "🔄 Step 4: Syncing in-app messages...")
174-
Thread.sleep(3000) // Give time for any messages to sync
179+
// Step 4: Wait for push-triggered sync (primary path)
180+
Log.d(TAG, "🔄 Step 4: Waiting for InAppUpdate push to trigger automatic sync...")
181+
val syncViaPush = waitForInAppSyncViaPush(initialMessageCount, pushTimeoutSeconds = 10)
175182

176-
// Manually sync
177-
IterableApiHelper().syncInAppMessages()
183+
var messageCount = IterableApi.getInstance().inAppManager.messages.count()
178184

179-
// Wait for sync to complete
180-
Thread.sleep(2000)
181-
182-
val messageCount = IterableApi.getInstance().inAppManager.messages.count()
183-
Log.d(TAG, "🔄 Message count after sync: $messageCount")
185+
// Step 4b: Fallback to manual sync if push didn't work
186+
if (!syncViaPush || messageCount == 0) {
187+
Log.d(TAG, "⚠️ Push-triggered sync did not complete, falling back to manual sync...")
188+
Log.d(TAG, "🔄 Step 4b: Manually syncing in-app messages...")
189+
Thread.sleep(2000) // Give a bit more time in case push is still arriving
190+
191+
// Check again before manual sync
192+
messageCount = IterableApi.getInstance().inAppManager.messages.count()
193+
if (messageCount == 0) {
194+
IterableApiHelper().syncInAppMessages()
195+
Thread.sleep(2000) // Wait for sync to complete
196+
messageCount = IterableApi.getInstance().inAppManager.messages.count()
197+
Log.d(TAG, "🔄 Message count after manual sync: $messageCount")
198+
} else {
199+
Log.d(TAG, "✅ Messages synced via push (delayed), message count: $messageCount")
200+
}
201+
} else {
202+
Log.d(TAG, "✅ Messages synced via push-triggered automatic sync, message count: $messageCount")
203+
}
184204

185205
Assert.assertTrue(
186206
"Message count should be 1, but was $messageCount",

integration-tests/src/main/java/com/iterable/integration/tests/services/IntegrationFirebaseMessagingService.kt

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,35 @@ class IntegrationFirebaseMessagingService : FirebaseMessagingService() {
3232
Log.d(TAG, "Message data: ${remoteMessage.data}")
3333
Log.d(TAG, "Message notification: ${remoteMessage.notification}")
3434

35-
// Check if this is a silent push for in-app messages
36-
val isSilent = remoteMessage.data["silent"] == "true"
37-
val isInAppMessage = remoteMessage.data["inAppMessage"] == "true"
35+
// Let the Iterable SDK handle the message first
36+
// This will automatically call syncInApp() for InAppUpdate notifications
37+
val isIterableMessage = IterableFirebaseMessagingService.handleMessageReceived(this, remoteMessage)
3838

39-
if (isSilent && isInAppMessage) {
40-
Log.d(TAG, "Received silent push for in-app message")
41-
// The Iterable SDK will handle the silent push automatically
42-
// We just need to track that it was received
43-
IntegrationTestUtils(this).setSilentPushProcessed(true)
39+
if (isIterableMessage) {
40+
// Check if this is an InAppUpdate push notification
41+
val notificationType = remoteMessage.data["notificationType"]
42+
val isInAppUpdate = notificationType == "InAppUpdate"
43+
44+
if (isInAppUpdate) {
45+
Log.d(TAG, "Received InAppUpdate push notification - SDK automatically synced in-app messages")
46+
// Track that InAppUpdate push was received and processed
47+
IntegrationTestUtils(this).setSilentPushProcessed(true)
48+
} else {
49+
// Check if this is a silent push for in-app messages (legacy check)
50+
val isSilent = remoteMessage.data["silent"] == "true"
51+
val isInAppMessage = remoteMessage.data["inAppMessage"] == "true"
52+
53+
if (isSilent && isInAppMessage) {
54+
Log.d(TAG, "Received silent push for in-app message")
55+
IntegrationTestUtils(this).setSilentPushProcessed(true)
56+
} else {
57+
Log.d(TAG, "Received regular Iterable push notification")
58+
// Regular push notification - Iterable SDK will handle display
59+
IntegrationTestUtils(this).setPushNotificationReceived(true)
60+
}
61+
}
4462
} else {
45-
Log.d(TAG, "Received regular push notification")
46-
// Regular push notification - Iterable SDK will handle display
47-
IntegrationTestUtils(this).setPushNotificationReceived(true)
48-
IterableFirebaseMessagingService.handleMessageReceived(this, remoteMessage)
63+
Log.d(TAG, "Received non-Iterable push notification")
4964
}
5065
}
5166

integration-tests/src/main/java/com/iterable/integration/tests/utils/IntegrationTestUtils.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class IntegrationTestUtils(private val context: Context) {
8080
fun setSilentPushProcessed(processed: Boolean) {
8181
silentPushProcessed.set(processed)
8282
}
83+
84+
fun isSilentPushProcessed(): Boolean {
85+
return silentPushProcessed.get()
86+
}
8387

8488
fun setInAppMessageDisplayed(displayed: Boolean) {
8589
inAppMessageDisplayed.set(displayed)

0 commit comments

Comments
 (0)