Sending SMS messages
We may be creating an app that requires the user to send an SMS message to a specific number.
How to do it...
To send an SMS message, we make use of the SmsManager
instance:
Before we can work with the
SmsManager
instance, we require a permission:[assembly: UsesPermission(Manifest.Permission.SendSms)]
To send the SMS, we only require a single method—
SendTextMessage
:var number = "0123456789"; var message = "SMS Message Text..."; var manager = SmsManager.Default; manager.SendTextMessage(number, null, message, null, null);
If we need to verify that the message was sent successfully, we can use intents that will be broadcast with the operation status:
First, we have to send the message providing the intents that will be broadcast when the message is sent and delivered:
var sentIntent = PendingIntent.GetBroadcast( this, 0, new Intent("xamarincookbook.Send"), 0); var deliveredIntent = PendingIntent.GetBroadcast( this, 0, new Intent("xamarincookbook.Delivered"), 0); manager.SendTextMessage...