Hybrid App Setup - Android SDK (v4)

    注意

    为了使用此功能,必须完成 2 个 SDK 安装:移动原生环境中的 Airbridge Android SDK 和 WebView 环境网站上的 Airbridge Web SDK。

    You can configure the Android SDK to handle Airbridge-related tasks within the hybrid app environment when they occur on the in-app website without requiring updates to the website code.

    Manage Web SDK Commands with the Android SDK

    The Android SDK can handle the commands related to event transmission, device settings, and user settings carried out by the Web SDK. Set the Airbridge.setWebInterface function before displaying the website on WebView.

    YOUR_WEB_TOKENis the Web SDK token. You can find it on the [Settings]>[Tokens] page in the Airbridge dashboard.

    12345678910111213141516171819
    class MainActivity : Activity() {
        
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            initWebView()
        }
        
        fun initWebView() {
            webView.settings.javaScriptEnabled = true
            webView.settings.domStorageEnabled = true
            
            Airbridge.setWebInterface(webView, "YOUR_WEB_TOKEN")
    
            webView.webChromeClient = WebChromeClient()
            webView.webViewClient =  WebViewClient()
            webView.loadUrl("http://my_company.com/main")
        }
    }

    Add the Airbridge.click function to enable deep links in WebView. By default, WebView does not support deep links, so you need to use the function to configure the SDK to enable the deep linking feature of the Airbridge tracking links.

    If the input address is a tracking link, the Airbridge.click function returns true and calls the onSuccess function when the app is launched through the address. If there is no internet connection or a different app is launched, the onFailure function is called.

    If the input address is not a tracking link, the Airbridge.click function returns false , and no further action is executed.

    12345
    fun click(
        trackingLink: String,
        onSuccess: OnSuccess<Unit>? = null,
        onFailure: OnFailure? = null
    ): Boolean

    By implementing WebViewClient#shouldOverrideUrlLoading in the WebViewClient, you can decide whether to continue loading a specific URL after verifying it is loading. For more information, refer to the Android Developer Guide.

    123456789101112131415161718
    private val webViewClient = object : WebViewClient() {
    
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return super.shouldOverrideUrlLoading(view, url)
        }
    
        @RequiresApi(Build.VERSION_CODES.N)
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            return super.shouldOverrideUrlLoading(view, request)
        }
    }
    
    private fun initWebview() {
        webView.webViewClient = webViewClient
    }

    During loading, if a specific URL is an Airbridge tracking link, you can use the Airbridge.click function to send the result of webView#shouldOverrideUrlLoading as true. This allows the Airbridge Android SDK to open the tracking link instead of the webview.

    The Airbridge tracking link is a URL of which the host is abr.ge , YOUR_APP_NAME.airbridge.io, or a custom domain.

    12345678910111213141516171819202122232425262728293031323334
    private val webViewClient = object : WebViewClient() {
    
        private fun handleDomain(view: WebView, uri: Uri): Boolean =
            // return true when uri is tracking link
            // return false when uri is not tracking link
            Airbridge.click(uri.toString(), {
                // on success
                // do nothing
            }, {
                // on failure
                // when uri is another app's tracking link or sdk fails to open uri
                view.loadUrl(uri.toString())
            })
    
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            if (view === null) return false
            if (url == null) return false
    
            val uri = Uri.parse(url) ?: return false
            return handleDomain(view, uri)
        }
    
        @RequiresApi(Build.VERSION_CODES.N)
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            if (view === null) return false
    
            val uri = request?.url ?: return false
            return handleDomain(view, uri)
        }
    }
    

    Attention

    Was this page helpful?

    Have any questions or suggestions?