skip to Main Content

On my website, the user must accept cookies before proceeding. There is an API for checking consent:

(window.consentApi?.consent("tidio-chat") || Promise.resolve()).then(() => {
    console.log("Consent given, do something...!");
});

This callback is invoked once the user has accepted all cookies.

How can I listen to this callback from my Android app via webview? I have tried to use evaluateJavascript but that won’t work.

I suspect I have to use addJavascriptInterface but I don’t know how to use it with || conditions. Could anyone give me a hint? Thank you.

2

Answers


  1. Handle android side like this :

    public class MyActivity extends AppCompatActivity {
    
    private WebView myWebView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        myWebView = findViewById(R.id.myWebView);
        myWebView.getSettings().setJavaScriptEnabled(true);
        CookieManager.getInstance().setAcceptCookie(true);
    
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    
        // Load your website URL
        myWebView.loadUrl("https://your-website.com");
    }
    
    public class WebAppInterface {
        Context mContext;
    
        WebAppInterface(Context c) {
            mContext = c;
        }
    
        @JavascriptInterface
        public void notifyConsent() {
            // Do something when consent is given (e.g., Toast)
            Toast.makeText(mContext, "Cookies Accepted!", Toast.LENGTH_SHORT).show();
        }
    }
    

    }

    In your web api do changes like this :

     (window.consentApi?.consent("tidio-chat") || Promise.resolve()).then(() => {
        console.log("Consent given, do something...!");
        if (window.Android) {
            window.Android.notifyConsent();
        }
    });
    
    Login or Signup to reply.
  2. To listen to the callback from your Android app via WebView, you can indeed use addJavascriptInterface to communicate between JavaScript and Java code. Here’s how you can achieve this:

    1. Define a JavaScript interface that will be accessible from your Android app:
    public class ConsentInterface {
        @JavascriptInterface
        public void onConsentGiven() {
            // This method will be called when consent is given
            Log.d("Consent", "Consent given, do something...!");
            // Add your logic here
        }
    }
    
    1. Add the JavaScript interface to your WebView:
    WebView webView = findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new ConsentInterface(), "Android");
    
    1. Modify your JavaScript code to call the Java method when consent is given:
    (window.consentApi?.consent("tidio-chat") || Promise.resolve()).then(() => {
        // Call the Java method when consent is given
        Android.onConsentGiven();
    });
    

    With this setup, when the consent API is called and the user accepts cookies, the onConsentGiven method in your ConsentInterface class will be invoked from the JavaScript code. You can then perform any desired actions within that Java method.

    Make sure to replace R.id.webView with the ID of your WebView if you haven’t already. Additionally, ensure that you handle potential security implications of using addJavascriptInterface by restricting the JavaScript interface to specific methods and classes as needed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search