0

My Target: By pressing a button, a whatsapp video call to a specific person starts. I don't know Java well enough, so I would like to accomplish this via C# in Unity.

I tried to replicate this process by translating it into C#.

But there is something wrong, because once the button is pressed I can only access the whatsapp home.

I also added permissions to access contacts from the manifest.

I'm doing everything manually, so I pasted the script on android studio to find the _id of a specific contact that had the mimetype for whatsapp video calls.

So I tried to translate the call function via Unity's AndroidJavaClass:

Java code:

 public void videoCall(String id){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);

            intent.setDataAndType(Uri.parse("content://com.android.contacts/data/" + id),
                    "vnd.android.cursor.item/vnd.com.whatsapp.video.call");
            intent.setPackage("com.whatsapp");

            startActivity(intent);
}

C# code:

public static class Intentions
{
static string id = "5576";
static string data_url = "content://com.android.contacts/data/" + id;
static string type_url = "vnd.android.cursor.item / vnd.com.whatsapp.video.call";

public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> 
("currentActivity");
public static AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject> 
("getPackageManager");
public static AndroidJavaObject intent = packageManager.Call<AndroidJavaObject> 
("getLaunchIntentForPackage", "com.whatsapp");

public static AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
public static AndroidJavaObject uriData = uriClass.CallStatic<AndroidJavaObject>("parse", data_url);

public static void Launch()
{
    intent.Call<AndroidJavaObject>("setAction", "android.intent.action.VIEW");
    intent.Call<AndroidJavaObject>("setDataAndType", uriData, type_url);

    if (IsAndroid())
    {
        currentActivity.Call("startActivity", intent);
    }
}

public static bool IsAndroid()
{
#if UNITY_ANDROID && !UNITY_EDITOR
    return true;
#else
    return false;
#endif
}
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Eugenio
  • 118
  • 1
  • 11

1 Answers1

0

You actually don’t need to do a plug-in thru Java but can Use C# directly from your Unity source code. Do this to make calling WhatsApp API call via REST interface.

String number = “16696661337”;
String url = "https://api.whatsapp.com/send?phone=" + number;
/* 
mobile number is: 1-(669)666-1337, your final string will be:
https://api.whatsapp.com/send?phone=6696661337
*/
Application.OpenURL(url);

You can also send text messages too if you want to. Don’t forget to include your country code for the API to work as intended.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • Thanks, unfortunately this method only allows you to reach the whatsapp contact profile, after which it will be necessary to manually press on the video call symbol to start it. I was looking for a method to reach that contact and directly initiate a video call. – Eugenio Feb 10 '21 at 16:20