I want to used Whatsapp as customer service. My apps is a webview, when the customer going to contact page, and see a number whatsapp and Chat Us button to chat with customer service.
When customer click the button, their didn't direct open whatsapp and showing ERR_UNKOWN_SCHEME_URL
I'm using android studio and webview to my apps using kotlin.
This is my activity :
class MainActivity : AppCompatActivity() {
internal var webView: WebView? = null
private var mCM: String? = null
private var mUM: ValueCallback<Uri>? = null
private var mUMA: ValueCallback<Array<Uri?>>? = null
@SuppressLint("SetJavaScriptEnabled", "WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.ifView) as WebView
assert(webView != null)
val webSettings = webView!!.settings
webSettings.javaScriptEnabled = true
webSettings.allowFileAccess = true
if (Build.VERSION.SDK_INT >= 21) {
webSettings.mixedContentMode = 0
webView!!.setLayerType(View.LAYER_TYPE_HARDWARE, null)
}
else {
webView!!.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
webView!!.webViewClient = Callback()
webView!!.loadUrl("http://example.com") //add your test web/page address here
webView!!.setWebChromeClient(object : WebChromeClient() {
SuppressWarnings("deprecation")
@Override
fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean
{
if (url != null && url.startsWith("whatsapp://")) {
val sendIntent = Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Moon TV");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent)
return true
/* ContextCompat.startActivity(sendIntent);return true;*/
} else {
return false;
}
}
This is My Callback() code :
inner class Callback : WebViewClient() {
override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
Toast.makeText(applicationContext, "No Internet Access!", Toast.LENGTH_SHORT).show()
view.loadUrl("file:///android_asset/error.html")
}
}
This is php code to Open Direct Chat Whatsapps Customer Service
<a href="https://api.whatsapp.com/send?phone=628118906771&text=Halo%20Example," class="btn btn-success btn-md">Visit WhatsApp</a>
I want Intent WhatsApp In Webview. Thank You.
from How to Intent Whatsapp Opened In Webview?
No comments:
Post a Comment