Skip to content

Commit f5833f9

Browse files
author
“Akshay
committed
Using server campaign send in InApp Activity
1 parent 46256fa commit f5833f9

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

integration-tests/src/main/java/com/iterable/integration/tests/activities/InAppMessageTestActivity.kt

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ import com.iterable.iterableapi.IterableInAppLocation
1313
import com.iterable.integration.tests.R
1414
import com.iterable.integration.tests.BuildConfig
1515
import com.iterable.iterableapi.IterableApiHelper
16+
import com.iterable.integration.tests.utils.IntegrationTestUtils
17+
import com.iterable.integration.tests.TestConstants
1618

1719
class InAppMessageTestActivity : AppCompatActivity() {
1820

1921
companion object {
2022
private const val TAG = "InAppMessageTest"
23+
private const val TEST_INAPP_CAMPAIGN_ID = TestConstants.TEST_INAPP_CAMPAIGN_ID
2124
}
2225

26+
private lateinit var testUtils: IntegrationTestUtils
2327
private lateinit var userInfoTextView: TextView
2428
private lateinit var statusTextView: TextView
2529
private lateinit var triggerButton: Button
@@ -34,6 +38,8 @@ class InAppMessageTestActivity : AppCompatActivity() {
3438

3539
Log.d(TAG, "In-App Message Test Activity started")
3640

41+
testUtils = IntegrationTestUtils(this)
42+
3743
initializeViews()
3844
setupClickListeners()
3945
setupWebView()
@@ -84,20 +90,35 @@ class InAppMessageTestActivity : AppCompatActivity() {
8490
}
8591

8692
private fun triggerInAppMessage() {
87-
updateStatus("Triggering in-app message...")
93+
updateStatus("Triggering in-app campaign via server API...")
8894

8995
try {
90-
// Track an event that should trigger an in-app message
91-
IterableApi.getInstance().track("test_inapp_event")
96+
val userEmail = IterableApi.getInstance().getEmail() ?: TestConstants.TEST_USER_EMAIL
9297

93-
// Also try to get existing messages and display them
94-
val messages = IterableApi.getInstance().getInAppManager().getMessages()
95-
if (messages.isNotEmpty()) {
96-
Log.d(TAG, "Found ${messages.size} in-app messages")
97-
updateStatus("Found ${messages.size} in-app messages - displaying first one")
98-
displayInAppMessage(messages.first())
99-
} else {
100-
updateStatus("No in-app messages available - tracked event to potentially trigger new ones")
98+
Log.d(TAG, "Triggering campaign $TEST_INAPP_CAMPAIGN_ID for user $userEmail")
99+
100+
// Trigger campaign via server API (like CampaignTriggerTestActivity does)
101+
testUtils.triggerCampaignViaAPI(TEST_INAPP_CAMPAIGN_ID, userEmail) { success ->
102+
runOnUiThread {
103+
if (success) {
104+
Log.d(TAG, "✅ Campaign triggered successfully")
105+
updateStatus("✅ Campaign triggered! Syncing messages...")
106+
107+
// Wait a bit for the campaign to be processed on the server
108+
Thread {
109+
Thread.sleep(2000)
110+
111+
runOnUiThread {
112+
// Refresh messages to get the newly triggered campaign
113+
refreshInAppMessages()
114+
}
115+
}.start()
116+
} else {
117+
val errorMessage = testUtils.getLastErrorMessage()
118+
Log.e(TAG, "❌ Failed to trigger campaign: $errorMessage")
119+
updateStatus("❌ Failed to trigger campaign: $errorMessage")
120+
}
121+
}
101122
}
102123
} catch (e: Exception) {
103124
Log.e(TAG, "Error triggering in-app message", e)
@@ -148,16 +169,24 @@ class InAppMessageTestActivity : AppCompatActivity() {
148169

149170
private fun refreshInAppMessages() {
150171
try {
151-
IterableApiHelper().syncInAppMessages()
152172
updateStatus("Refreshing in-app messages...")
173+
IterableApiHelper().syncInAppMessages()
153174
updateUserInfo() // Also refresh user info
154175

155-
// Check current message count
156-
val messages = IterableApi.getInstance().getInAppManager().getMessages()
157-
val inboxMessages = IterableApi.getInstance().getInAppManager().getInboxMessages()
158-
159-
updateStatus("Found ${messages.size} in-app messages, ${inboxMessages.size} inbox messages")
160-
Log.d(TAG, "Message refresh: ${messages.size} in-app, ${inboxMessages.size} inbox")
176+
// Wait a bit for sync to complete
177+
Thread {
178+
Thread.sleep(2000)
179+
180+
runOnUiThread {
181+
// Check current message count
182+
val messages = IterableApi.getInstance().getInAppManager().getMessages()
183+
val inboxMessages = IterableApi.getInstance().getInAppManager().getInboxMessages()
184+
185+
updateStatus("Found ${messages.size} in-app messages, ${inboxMessages.size} inbox messages")
186+
Log.d(TAG, "Message refresh: ${messages.size} in-app, ${inboxMessages.size} inbox")
187+
188+
}
189+
}.start()
161190

162191
} catch (e: Exception) {
163192
Log.e(TAG, "Error refreshing in-app messages", e)

0 commit comments

Comments
 (0)