我想向WhatsApp中的特定号码发送消息或通过短信发送消息,并且在没有该号码或WhatsApp自定义的情况下,出现错误。
使用软件包
Xamarin.Forms.OpenWhatsApp
我的代码不起作用
var sendwhats = FindViewById<Button>(Resource.Id.button3);
sendwhats.Click += (sender, e) => {
var phone = textinp.Text;
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.SetComponent(new ComponentName("com.whatsapp", "com.whatsapp.Conversation"));
sendIntent.PutExtra("jid", PhoneNumberUtils.StripSeparators(phone) + "@s.whatsapp.net");without "+" prefix
StartActivity(sendIntent);
};
答案 0 :(得分:0)
首先,Xamarin.Forms.OpenWhatsApp
此插件用于Xamarin.Forms项目,不能在Xamarin.Android项目中使用。
然后,如果您使用Xamarin.Android项目,则可以使用以下代码发送消息。
private void Button1_Click(object sender, System.EventArgs e)
{
string phoneNumberWithCountryCode = "88167xxxxxxx";
string message = "Hallo";
StartActivity(new Intent(Intent.ActionView,
Uri.Parse("https://api.whatsapp.com/send?phone=" + phoneNumberWithCountryCode + "&text=" + message)));
}
如果电话号码未注册或不存在,whatsapp将向您发出警报。由于私人政策,我无法给您提供测试GIF,您可以自己进行测试。