1

I am developing a NativeScript Android application in which I want the user to be able to open a WhatsApp contact after pressing a button (only phone number is known). I currently use Nativescript-open-app to open WhatsApp. Is it possible to also open the conversation?

To open WhatsApp I use the following code (maybe it's possible by altering "com.whatsapp"?):

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

Any help is welcome, thank you!

Stefan
  • 323
  • 2
  • 15
  • Take a look here on how to open specific conversation in Whatsapp.. https://stackoverflow.com/a/42186108/3866010. `drupe` is an Android phone dialer app that also does this – ᴛʜᴇᴘᴀᴛᴇʟ Apr 22 '18 at 14:59

2 Answers2

3

import * as appavailability from "nativescript-appavailability";
import { openUrl } from "tns-core-modules/utils/utils";

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();
        }
      })
  }
}
Jay Thakkar
  • 1,392
  • 10
  • 19
1
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();
   }  
}