In the previous blogs, we learnt about how can we trigger native Android text, image or any other format file sharing in Unity App. A lot of you have asked me about how can we share some text directly to a WhatsApp contact.
If you have not read the previous medium posts, I would strongly recommend to read them first. You can read them on the links below.
- Native Android in Unity
- Native Android text sharing in Unity
- Native Android image sharing in Unity
- Native Android text sharing to particular app in Unity
Whatsapp is something which doesn’t require any introduction. It’s a conversation app used by millions of users across the world. These million users make it unique for developers also to give special attention when it comes to sharing some message through Whatsapp.
During recent times, I was asked multiple times in the comments section or even emails, if there is any possible way where we can send the text message directly to a WhatsApp contact?
The ideal way any app accepts text, image, video or any other format for sharing is through intent filters defined in the manifest file.
Whatsapp also does the same and accepts all possible format (with some size and other limitations) but that will take you to a contact selection screen in Whatsapp.
If you want to share the text directly to a contact on WhatsApp, there is a hack which is actually not documented anywhere but using the same we can achieve it.
There is an API from WhatsApp which accepts phone
and text
and query parameters and if you call that API, it actually opens WhatsApp, check if that number exists on WhatsApp, if yes it opens conversation with that number and paste the message. If the contact or mobile numbers doesn’t exist on WhatsApp, it shows a popup saying contact doesn’t exist on WhatsApp.
Kotlin code
fun onWhatsAppShareClicked(context: Context, mobileNumber: String) {
val url =
"https://api.whatsapp.com/send?phone=${mobileNumber}&text=You%20can%20now%20send%20me%20audio%20and%20video%20messages%20on%20the%20app%20-%20Chirp.%20%0A%0Ahttps%3A//bit.ly/chirp_android"
val intent = Intent(Intent.ACTION_VIEW).apply {
this.data = Uri.parse(url)
this.`package` = "com.whatsapp"
}
try {
context.startActivity(intent)
} catch (ex : ActivityNotFoundException){
//whatsapp not installled
}
}
Things to notice here
- We are not triggering the share intent. Instead, we are triggering an Action_VIEW intent with a URI which holds our url to hit.
- We are setting the package name exclusively to WhatsApp package in the intent.
- Before triggering this you need to check whether Whatsapp is installed or not using below code.
Limitations
- We can only share text in this format. we can’t pass an image, video or any other format in this way. But we can pass an URL to an image which can be on the server.
Now let’s look at the equivalent code in Unity.
//C# code
#if UNITY_ANDROID
public IEnumerator ShareTextToWhatsContact() {
isProcessing = true;
if (!Application.isEditor) {
//var url = "https://api.whatsapp.com/send?phone=${mobileNumber}&text=You%20can%20now%20send%20me%20audio%20and%20video%20messages%20on%20the%20app%20-%20Chirp.%20%0A%0Ahttps%3A//bit.ly/chirp_android";
var url = "https://api.whatsapp.com/send?phone=+919876543210&text=You%20can%20now%20send%20me%20audio%20and%20video%20messages%20on%20the%20app%20-%20Chirp.%20%0A%0Ahttps%3A//bit.ly/chirp_android";
//Create intent for action send
AndroidJavaClass intentClass =
new AndroidJavaClass ("android.content.Intent");
AndroidJavaObject intentObject =
new AndroidJavaObject ("android.content.Intent");
intentObject.Call<AndroidJavaObject>
("setAction", intentClass.GetStatic<string> ("ACTION_VIEW"));
//uri class
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
//set data
intentObject.Call<AndroidJavaObject>("setData",
uriClass.CallStatic<AndroidJavaObject>("parse", url));
//set the package to whatsapp package
intentObject.Call<AndroidJavaObject> ("setPackage", packageName);
//call start activity method
AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity =
unity.GetStatic<AndroidJavaObject> ("currentActivity");
currentActivity.Call ("startActivity", intentObject);
}
yield return new WaitUntil (() => isFocus);
isProcessing = false;
}
#endif
If you have read my previous unity blogs, you’ll understand very easily what we are doing here. We just used AndroidJavaClass and AndroidJavaObject and created the same intent object with action as ACTION_VIEW and set the data as the Uri object by parsing the url and finally triggered the startActivity
function on the context.
The final script will look like below.