【问题标题】:Nativescript send message through WhatsAppNativescript 通过 WhatsApp 发送消息
【发布时间】:2018-04-22 14:38:30
【问题描述】:

我正在开发一个 NativeScript Android 应用程序,我希望用户能够在按下按钮后打开 WhatsApp 联系人(仅知道电话号码)。我目前使用Nativescript-open-app 打开 WhatsApp。是否也可以打开对话?

要打开 WhatsApp,我使用以下代码(也许可以通过更改“com.whatsapp”?):

var openApp = require("nativescript-open-app").openApp;
var installed = openApp("com.whatsapp");

欢迎任何帮助,谢谢!

【问题讨论】:

  • 在此处查看如何在 Whatsapp 中打开特定对话.. stackoverflow.com/a/42186108/3866010drupe 是一个 Android 电话拨号器应用程序,它也可以做到这一点

标签: android nativescript whatsapp


【解决方案1】:

从“nativescript-appavailability”导入 * 作为应用可用性;
从“tns-core-modules/utils/utils”导入 { openUrl };

if(this..whatsapp_number != null) {
  if(topmost().ios) {
    appavailability.available("whatsapp://")
      .then((avail: boolean) => {
        if(avail) {
          openUrl("whatsapp://send?phone="+this..whatsapp_number)
        } else {
          Toast.makeText("Whatsapp is not installed").show();
        }
      })
  } else {
    appavailability.available("com.whatsapp")
      .then((avail: boolean) => {
        if(avail) {
          openUrl("https://api.whatsapp.com/send?phone="+this.whatsapp_number)
        } else {
          Toast.makeText("Whatsapp is not installed").show();
        }
      })
  }
}

【讨论】:

  • 为什么必须检查 'whatsapp://' 和 'com.whatsapp' 的可用性?
  • 我已经检查了 iOS 和 Android。
  • *两次可用性检查
【解决方案2】:
public void onClickWhatsApp(View view) {

    PackageManager pmA=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "Text of massage";

        PackageInfo info=pmA.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share to"));

   } catch (NameNotFoundException e) {
        Toast.makeText(this, "pleas Install WhatsApp", Toast.LENGTH_SHORT)
                .show();
   }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多