diff --git a/messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt b/messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt index 146715da2..0f68ff7df 100644 --- a/messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt +++ b/messaginginapp/src/main/java/io/customer/messaginginapp/gist/data/listeners/Queue.kt @@ -15,6 +15,7 @@ import java.util.regex.PatternSyntaxException import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import okhttp3.Cache +import okhttp3.Headers import okhttp3.OkHttpClient import okhttp3.ResponseBody.Companion.toResponseBody import retrofit2.Retrofit @@ -132,6 +133,9 @@ class Queue : GistListener { ) latestMessagesResponse.body()?.let { handleMessages(it) } } + + // Check if the polling interval changed and update timer. + updatePollingInterval(latestMessagesResponse.headers()) } catch (e: Exception) { Log.e( GIST_TAG, @@ -141,6 +145,20 @@ class Queue : GistListener { } } + private fun updatePollingInterval(headers: Headers) { + headers["X-Gist-Queue-Polling-Interval"]?.toIntOrNull()?.let { pollingIntervalSeconds -> + if (pollingIntervalSeconds > 0) { + val newPollingIntervalMilliseconds = (pollingIntervalSeconds * 1000).toLong() + if (newPollingIntervalMilliseconds != GistSdk.pollInterval) { + GistSdk.pollInterval = newPollingIntervalMilliseconds + // Queue check fetches messages again and could result in infinite loop. + GistSdk.observeMessagesForUser(true) + Log.i(GIST_TAG, "Polling interval changed to: $pollingIntervalSeconds seconds") + } + } + } + } + private fun handleMessages(messages: List) { // Sorting messages by priority and placing nulls last val sortedMessages = messages.sortedWith(compareBy(nullsLast()) { it.priority }) diff --git a/messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt b/messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt index 9b51a2f44..dacaf4268 100644 --- a/messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt +++ b/messaginginapp/src/main/java/io/customer/messaginginapp/gist/presentation/GistSdk.kt @@ -25,12 +25,12 @@ const val GIST_TAG: String = "[CIO]" object GistSdk : Application.ActivityLifecycleCallbacks { private const val SHARED_PREFERENCES_NAME = "gist-sdk" private const val SHARED_PREFERENCES_USER_TOKEN_KEY = "userToken" - private const val POLL_INTERVAL = 10_000L private val sharedPreferences by lazy { application.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE) } + internal var pollInterval = 600_000L lateinit var siteId: String lateinit var dataCenter: String internal lateinit var gistEnvironment: GistEnvironment @@ -187,16 +187,18 @@ object GistSdk : Application.ActivityLifecycleCallbacks { // Gist Message Observer - private fun observeMessagesForUser() { + internal fun observeMessagesForUser(skipQueueCheck: Boolean = false) { // Clean up any previous observers observeUserMessagesJob?.cancel() Log.i(GIST_TAG, "Messages timer started") - gistQueue.fetchUserMessages() + if (!skipQueueCheck) { + gistQueue.fetchUserMessages() + } observeUserMessagesJob = GlobalScope.launch { try { // Poll for user messages - val ticker = ticker(POLL_INTERVAL, context = this.coroutineContext) + val ticker = ticker(pollInterval, context = this.coroutineContext) for (tick in ticker) { gistQueue.fetchUserMessages() }