返回

安卓中 FrontApp LiveChat 隐藏 WebView 疑难杂症指南

Android

如何在安卓中无缝集成 FrontApp LiveChat

问题概述

集成 FrontApp LiveChat 至安卓应用时,您可能会遇到一个棘手的难题:WebView 无法在关闭聊天会话窗口后隐藏。这可能导致用户界面出现不一致的问题,并且会损害整体用户体验。

解决方案

解决此问题需要遵循以下步骤:

  • 验证指南: 仔细检查 FrontApp 的集成指南,确保您的代码与之完全一致。
  • 自定义 WebViewClient: 创建自定义 WebViewClient,并覆盖其 shouldOverrideUrlLoading 方法。
  • 隐藏 WebView:shouldOverrideUrlLoading 方法中,隐藏 WebView 并显示一个重新打开聊天窗口的按钮。
  • 显示重新打开按钮: 添加一个按钮,在点击后将隐藏自身并显示 WebView,以便用户重新打开聊天窗口。

代码示例

以下代码片段展示了如何实现上述解决方案:

class MainActivity : ComponentActivity() {
    private var fileChooserResultLauncher = createFileChooserResultLauncher()
    private var fileChooserValueCallback: ValueCallback<Array<Uri>>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        CookieManager.getInstance().setAcceptCookie(true)
        CookieManager.getInstance().removeAllCookies(null)
        WebStorage.getInstance().deleteAllData()
        val webView = findViewById<View>(R.id.web) as WebView

        webView.settings.javaScriptEnabled = true
        webView.settings.domStorageEnabled = true
        webView.settings.allowContentAccess = true
        webView.settings.allowFileAccess = true

        webView.webChromeClient = object : WebChromeClient() {
            override fun onShowFileChooser(
                webView: WebView?,
                filePathCallback: ValueCallback<Array<Uri>>?,
                fileChooserParams: FileChooserParams?
            ): Boolean {
                try {
                    fileChooserValueCallback = filePathCallback
                    fileChooserResultLauncher.launch(fileChooserParams?.createIntent())
                } catch (e: ActivityNotFoundException) {
                    e.printStackTrace()
                }
                return true
            }
        }

        val autoOpen =
            "<html>"+
                    "<body>" +
                    "<script src=\"https://chat.frontapp.com/v1/chat.bundle.js\"></script>" +
                    "<script>" +
                    "window.FrontChat('init', {chatId: 'ce4c58cd0f7cbcd3ed3eaf3b0b09XXXX', useDefaultLauncher: false, shouldMobileReloadOnClose: true})" +
                    "</script>" +
                    "<script>" +
                    "setTimeout(() => window.FrontChat('show'), 2000)" +
                    "</script>" +
                    "</body>" +
                    "</html>"

        webView.visibility = View.INVISIBLE

        webView.loadDataWithBaseURL("http://localhost/", autoOpen, "text/html", "UTF-8", null)

        val openChatButton: Button = findViewById<View>(R.id.button) as Button

        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                if (url == "https://chat.frontapp.com/close") {
                    webView.visibility = View.INVISIBLE
                    openChatButton.visibility = View.VISIBLE
                    return true
                }
                return false
            }
        }

        openChatButton.setOnClickListener {
            openChatButton.visibility = View.INVISIBLE
            webView.visibility = View.VISIBLE
        }
    }

    private fun createFileChooserResultLauncher(): ActivityResultLauncher<Intent> {
        return registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Activity.RESULT_OK) {
                fileChooserValueCallback?.onReceiveValue(arrayOf(Uri.parse(it?.data?.dataString)))
            } else {
                fileChooserValueCallback?.onReceiveValue(null)
            }
        }
    }
}

结论

通过遵循本文中概述的步骤,您将能够解决安卓应用程序中 FrontApp LiveChat 的 WebView 隐藏问题。此解决方案将确保您的应用程序提供流畅、无缝的聊天体验,提高用户满意度和参与度。

常见问题解答

1. 为什么我的 WebView 仍然无法隐藏?

  • 确保您正确设置了 shouldOverrideUrlLoading 方法,并且在 https://chat.frontapp.com/close URL 加载到 WebView 时返回了 true

2. 我需要做什么来使用 FrontApp 的其他功能?

  • FrontApp 为开发者提供了广泛的 API,允许您自定义聊天窗口的外观和功能。请参阅 FrontApp 文档以获取更多详细信息。

3. 如何处理安全问题?

  • 确保您遵循了 FrontApp 的安全指南,以保护用户数据和防止恶意活动。

4. 存在其他方法来解决此问题吗?

  • 虽然本文提供了行之有效的解决方案,但您还可以探索其他方法,例如使用前端框架或原生安卓组件。

5. 如何在不使用按钮的情况下隐藏 WebView?

  • 您可以使用 JavaScript 侦听 window.onbeforeunload 事件,并在关闭聊天窗口时隐藏 WebView。