Search Unity

How to make a whatsapp video call with C# Unity

Discussion in 'Scripting' started by Askautmir, Feb 14, 2021.

Thread Status:
Not open for further replies.
  1. Askautmir

    Askautmir

    Joined:
    Jun 13, 2018
    Posts:
    6
    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:
    Code (CSharp):
    1. public static class Intentions
    2. {
    3. static string id = "5576";
    4. static string data_url = "content://com.android.contacts/data/" + id;
    5. static string type_url = "vnd.android.cursor.item / vnd.com.whatsapp.video.call";
    6.  
    7. public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    8. public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>
    9. ("currentActivity");
    10. public static AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>
    11. ("getPackageManager");
    12. public static AndroidJavaObject intent = packageManager.Call<AndroidJavaObject>
    13. ("getLaunchIntentForPackage", "com.whatsapp");
    14.  
    15. public static AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
    16. public static AndroidJavaObject uriData = uriClass.CallStatic<AndroidJavaObject>("parse", data_url);
    17.  
    18. public static void Launch()
    19. {
    20.     intent.Call<AndroidJavaObject>("setAction", "android.intent.action.VIEW");
    21.     intent.Call<AndroidJavaObject>("setDataAndType", uriData, type_url);
    22.  
    23.     if (IsAndroid())
    24.     {
    25.         currentActivity.Call("startActivity", intent);
    26.     }
    27. }
    28.  
    29. public static bool IsAndroid()
    30. {
    31. #if UNITY_ANDROID && !UNITY_EDITOR
    32.     return true;
    33. #else
    34.     return false;
    35. #endif
    36. }
    37. }
     
  2. sheemer

    sheemer

    Joined:
    Mar 24, 2021
    Posts:
    1
    Hi, I'm new here and while I was experimenting with something similar on this FMWhatsApp Apk modified version of WhatsApp I found this script which was very useful to me and I think you should try this script.

    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.
     
  3. Josephbaiju

    Josephbaiju

    Joined:
    Feb 24, 2022
    Posts:
    2
    how can i send messages in whatsapp using unity
     
  4. SamHks

    SamHks

    Joined:
    Nov 18, 2022
    Posts:
    1
    Thank you so much mate, this worked for me!
     
  5. marelemore

    marelemore

    Joined:
    Dec 16, 2022
    Posts:
    1
    But it didn't worked when i try on modified version but worked with official one.
     
Thread Status:
Not open for further replies.