Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Send SMS on Xamarin.Forms

I am developing Xamarin.Forms project, iOS, Android and Windows Phone.

My app ask the user to enter text Message and Phone number then on submit, I need to send SMS to the phone number.

I prefer to have a single implementation for all platforms.

like image 466
Mohamad Mahmoud Avatar asked Feb 14 '17 15:02

Mohamad Mahmoud


People also ask

How do I use xamarin forms in messages Center?

Publish a message MessagingCenter. Send<MainPage>(this, "Hi"); In this example the Send method specifies a generic argument that represents the sender. To receive the message, a subscriber must also specify the same generic argument, indicating that they are listening for a message from that sender.


2 Answers

You can use this open source plugin Xam.Plugins.Messaging https://github.com/cjlotz/Xamarin.Plugins

This is the provided example (from https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Details.md ) :

// Make Phone Call
var phoneDialer = CrossMessaging.Current.PhoneDialer;
if (phoneDialer.CanMakePhoneCall) 
    phoneDialer.MakePhoneCall("+272193343499");

// Send Sms
var smsMessenger = CrossMessaging.Current.SmsMessenger;
if (smsMessenger.CanSendSms)
   smsMessenger.SendSms("+27213894839493", "Well hello there from Xam.Messaging.Plugin");

var emailMessenger = CrossMessaging.Current.EmailMessenger;
if (emailMessenger.CanSendEmail)
{
    // Send simple e-mail to single receiver without attachments, bcc, cc etc.
    emailMessenger.SendEmail("[email protected]", "Xamarin Messaging Plugin", "Well hello there from Xam.Messaging.Plugin");

    // Alternatively use EmailBuilder fluent interface to construct more complex e-mail with multiple recipients, bcc, attachments etc. 
    var email = new EmailMessageBuilder()
      .To("[email protected]")
      .Cc("[email protected]")
      .Bcc(new[] { "[email protected]", "[email protected]" })
      .Subject("Xamarin Messaging Plugin")
      .Body("Well hello there from Xam.Messaging.Plugin")
      .Build();

    emailMessenger.SendEmail(email);
}   
like image 54
Daniel Avatar answered Oct 17 '22 01:10

Daniel


Microsoft now has this feature in their new all-encompasing https://docs.microsoft.com/en-us/xamarin/essentials NuGet package.

like image 36
Ian Vink Avatar answered Oct 17 '22 01:10

Ian Vink