服务器返回 JWT 死机时 Smooch 对话恢复

Smooch conversation recovery when server returning JWT's dies

我已经将 Smooch/Sunshine Conversations SDK 集成到我们的应用程序中。

在大多数情况下,它是有效的。但是我在失败场景中遇到了一些问题:

  1. 用户已登录(我们的服务和接吻)
  2. 我们的服务器端由于某种原因挂掉了,这意味着 JWT 暂时无法获取
  3. 对话视图显示“无法连接到服务器”(如预期的那样)
  4. 我们的服务器端恢复...根据请求返回有效的 JWT
  5. 用户尝试在应用程序中触发对话,他们继续无限期地看到“无法连接到服务器”(即使在从对话 activity 返回并返回对话后也是如此)。
  6. Smooch SDK 从未从中恢复。解决它的唯一方法是杀死并重新启动应用程序。

我使用的是最新的 SDK 版本 7.0.3 和 vanilla ConversationActivity(我没有对它或任何东西进行子分类)

我试过以下方法:

有什么想法吗?

代码:

        // This is in the Application class, as recommended
        fun initialiseSmooch(application: Application) {
            GlobalScope.launch {
                Log.i(TAG, "Initialising Smooch")

                val settings = Settings("INTEGRATION_ID")
                settings.authenticationDelegate = getAuthenticationDelegate()

                Smooch.init(application, settings, getInitialisationCallback())
            }
        }   

        private fun getInitialisationCallback(): (SmoochCallback.Response<InitializationStatus>) -> Unit {
            return { response ->
                if (response.data === InitializationStatus.SUCCESS) {
                    Log.i(TAG, "Smooch initialised successfully")
                } else {
                    Log.e(TAG, "Smooch initialization failed: ${response.error}")
                }
            }
        }

        /**
         * This basically tells the Smooch SDK what to do if the JWT is rejected. Basically it goes
         * and fetches a new token from our API.
         */
        private fun getAuthenticationDelegate(): AuthenticationDelegate {
            return AuthenticationDelegate(function = { authenticationError, authenticationCallback ->
                if (authenticationError != null && authenticationError.data != null) {
                    Log.w(TAG, authenticationError.data)
                }
                if(AppResources.repository.getUserId() == null){
                    Log.i(TAG, "Authentication error. User isn't logged in, so shouldn't be logged in to Smooch either.")
                    logoutSmoochUser()
                } else {
                    Log.i(TAG, "Authentication error. Getting new Smooch token.")
                    getSmoochToken { token -> authenticationCallback.updateToken(token) }
                }
            })
        }

        private fun getSmoochToken(callback: (String) -> Unit) {
          // Fetches token from API. If successful, callback is called
          // If unsuccessful, callback isn't called. This won't hang forever, it has a timeout.
        }


    // And to start the conversation
    private fun proceedToConversation() {
        ConversationActivity.builder().show(this)
    }

请确保您已实施身份验证委托以自动处理 JWT 过期的情况:https://docs.smooch.io/guide/authenticating-users/#expiring-jwts-on-sdks

一旦发出 JWT 的后端恢复正常,我将通过 login() 调用开始恢复尝试。

最后,10s JWT 的有效期确实很短。