【问题标题】:How to show a notice if whatsapp sharing is not available?如果 whatsapp 共享不可用,如何显示通知?
【发布时间】:2021-11-30 14:10:37
【问题描述】:

假设我在台式机上,或者在没有 WhatsApp 应用程序的 Android 上。我按下它,没有任何反应。应该显示一条通知消息,例如“安装 WhatsApp 应用程序!”或类似的东西。我只有这个:

<a href="whatsapp://send?text=text">Click here to share!</a>

我试过这些:

<a href="whatsapp://send/?phone=62812345678&amp;text=test" id="openWA">Send to WhatsApp</a>
<br>
<a href="viber://forward?text=paff" id="openV">Send to Viber</a>

<!-- Auto open on WebView and Firefox -->

<script>
    document.querySelector('#openWA').addEventListener('click', function() {
        var f = Date.now(),
            j = setTimeout(function() {
                if (Date.now() - f > 1250)
                    return;
                alert('WA not installed')
            }, 1e3);
    });
    document.querySelector('#openV').addEventListener('click', function() {
        var f = Date.now(),
            j = setTimeout(function() {
                if (Date.now() - f > 1250)
                    return;
                alert('VIBER not installed')
            }, 1e3);
    });
</script>

但它说“未安装”,无论是否安装。

【问题讨论】:

  • 你按什么却什么也没发生?
  • 我按那个“点击这里分享!”链接

标签: android share whatsapp


【解决方案1】:

考虑向原生 Android 发送某种类型的数据并进行处理 那里

使用下面的方法来查看应用程序是否安装在 Java 中 但是你需要知道应用的包名

public static boolean isAppInstalled(Context context, String packageName) {
    try {
        context.getPackageManager().getApplicationInfo(packageName, 0);
        return true;
    }
    catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

用法

if(isAppInstalled(context,"com.Whatsapp") == false){

      //show toast saying whatsapp is not available
}

您可以使用以下方法将数据从 webView 发送到原生层

  1. 在native层创建一个接口,使webview可以连接native层并传递数据。

class JSBridge(){
    @JavascriptInterface
    fun showMessageInNative(message:String){
        //Received message from webview in native, call isAppInstalled(...) here

        // for example 

        if(message == "whatsapp"){

             if(isAppInstalled(context,"com.whatsapp") == false){

                    //show toast saying whatsapp is not available
             }
         }
    }
  1. 从脚本层内部调用此方法。 (编辑 valueToBePassed 以发送 whatsapp 或 viber 之类的)
function sendMessage() {
  
      JSBridge.showMessageInNative(valueToBePassed);
  }
  1. 在配置网页视图时,我们需要像上面的JSBridge类一样设置JavaScript接口。
mWebViewComponent.settings.javaScriptEnabled = true
mWebViewComponent.addJavascriptInterface(JSBridge(),"JSBridge")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 2018-01-05
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多