I am writing a MAUI app for a small group of users, and this code used to work. We were able to generate whatsapp messages to send a code to new clients. But now we can't send whatsapp messages anymore.
The call to Launcher.Default.CanOpenAsync()
returns false, even though we have whatsapp on the phone.
The minSdkVersion is 29 and the targetSdkVersion is 33.
public static class WhatsApp
{
public static async Task<bool> SendAsync(string phoneNumber, string message)
{
if (phoneNumber.StartsWith("0"))
phoneNumber = "27" phoneNumber.Substring(1);
else if (phoneNumber.StartsWith(" "))
phoneNumber = phoneNumber.Substring(1);
var supportsUri = await Launcher.Default.CanOpenAsync($"whatsapp://send?phone= {phoneNumber}");
if (supportsUri)
{
await Launcher.Default.OpenAsync($"whatsapp://send?phone= {phoneNumber}&text={message}");
return true;
}
else
return false;
}
}
What do I need to add to my AndroidManifest.xml for this code to work?
CodePudding user response:
After android 11 you have to add this line in manifeast
<manifest package="com.example.app">
<queries>
//for normal whatsapp
<package android:name="com.whatsapp"/>
//for business whatsapp
<package android:name="com.whatsapp.w4b"/>
</queries>
</manifest>