diff --git a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt index 8d393bc..6df60be 100644 --- a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt +++ b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7IncomingCallQueue.kt @@ -159,7 +159,12 @@ object F7IncomingCallQueue { val prefs = prefs(context) cancelTimeout(context) cancelNotification(context) - prefs.edit().remove(KEY_QUEUE).remove(KEY_ACTIVE_TOKEN).remove(KEY_ACTIVE_AT).remove(KEY_ACTIVE_CALL).apply() + prefs.edit() + .remove(KEY_QUEUE) + .remove(KEY_ACTIVE_TOKEN) + .remove(KEY_ACTIVE_AT) + .remove(KEY_ACTIVE_CALL) + .apply() } } diff --git a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt index 6d98c89..ef97d9e 100644 --- a/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt +++ b/core/push/src/main/java/ru/forbion/f7cloud/core/push/F7PushRegistrar.kt @@ -17,6 +17,10 @@ object F7PushRegistrar { private const val TAG = "F7PushRegistrar" private const val PREFS = "f7push" private const val KEY_DEVICE_ID = "device_id" + private const val MAX_ATTEMPTS = 3 + + // Паузы перед 2-й и 3-й попытками (мс); экспоненциальный бэкофф. + private val BACKOFF_MS = longArrayOf(1000L, 3000L) fun getDeviceId(context: Context): String { val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) @@ -41,12 +45,40 @@ object F7PushRegistrar { .put("platform", "android") .put("clientApp", "f7cloud-mobile") .toString() - return try { - postJson(endpoint, session, body) - } catch (t: Throwable) { - Log.w(TAG, "register failed", t) - 0 + + var lastCode = 0 + for (attempt in 0 until MAX_ATTEMPTS) { + if (attempt > 0) { + val wait = BACKOFF_MS[(attempt - 1).coerceAtMost(BACKOFF_MS.size - 1)] + try { + Thread.sleep(wait) + } catch (_: InterruptedException) { + Thread.currentThread().interrupt() + return lastCode + } + } + lastCode = try { + postJson(endpoint, session, body) + } catch (t: Throwable) { + Log.w(TAG, "register attempt ${attempt + 1} failed", t) + 0 + } + if (!isRetryable(lastCode)) { + return lastCode + } + Log.w( + TAG, + "register attempt ${attempt + 1} got $lastCode, " + + if (attempt + 1 < MAX_ATTEMPTS) "retrying" else "giving up" + ) } + return lastCode + } + + // Транзиентные сбои: сетевое исключение (0), ошибка сервера (5xx), 408/429. + // 2xx — успех, 4xx (кроме 408/429) — постоянная ошибка (кривой app-password/запрос), повтор не поможет. + private fun isRetryable(code: Int): Boolean { + return code == 0 || code >= 500 || code == 408 || code == 429 } private fun postJson(endpoint: String, session: AuthSession, json: String): Int {