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
Handle android side like this :
}
In your web api do changes like this :
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:With this setup, when the
consent
API is called and the user accepts cookies, theonConsentGiven
method in yourConsentInterface
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 usingaddJavascriptInterface
by restricting the JavaScript interface to specific methods and classes as needed.