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:

  1. Before we can work with the SmsManager instance, we require a permission:
    [assembly: UsesPermission(Manifest.Permission.SendSms)]
  2. 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:

  1. 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(
      number, null, message, sentIntent, deliveredIntent);
  2. We then create a broadcast receiver that will listen for these intents:
    public class SmsSentReceiver : BroadcastReceiver {
      public override void OnReceive(
      Context context, Intent intent) {
        if (intent.Action == "xamarincookbook.Send") {
          var sendSuccess = ResultCode == Result.Ok;
        }
        if (intent.Action == "xamarincookbook.Delivered") {
          var deliverySuccess = ResultCode == Result.Ok;
        }
      }
    }
  3. The broadcast receiver can be registered dynamically as it will only be needed for a short while:
    receiver = new SmsSentReceiver();
    RegisterReceiver(
      receiver, new IntentFilter("xamarincookbook.Send"));
    RegisterReceiver(
      receiver, new IntentFilter("xamarincookbook.Delivered"));
  4. The broadcast receiver can then be unregistered when the message has been delivered successfully:
    UnregisterReceiver(receiver);

How it works...

Sending SMS messages can be done from within an Android app without user intervention.

Note

Like phone calls, working with SMS messages requires that the device supports the telephony features.

To send SMS messages, we first need to request the SendSms permission. This is required because sending an SMS message involves a cost for the user.

Once we have the permission, we can then obtain an instance of the SmsManager instance. We then pass the destination number and message to the SendTextMessage() method.

Note

To confirm that a message was delivered to a recipient, an intent can be provided to the SendTextMessage() method. When the message is delivered, the intent will be broadcast.

The second last parameter of the SendTextMessage() method is a PendingIntent instance that will be broadcast if the message is successfully sent out or if there is an error in sending. The ResultCode will either be Ok if it is successful, or another value if there is an error.

To confirm that the SMS is delivered to the recipient, we provide a PendingIntent instance to the last parameter. This intent will be broadcast when the message has been delivered.

There's more...

If the message is going to be too long for a single message, we can use the DivideMessage() method on the SmsManager instance to break the message up into fragments.

The fragments are then sent to a recipient using the SendMultipartTextMessage() method. We pass the collection of fragments along with a collection of pending intents that should be broadcast as each fragment is sent and delivered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.1.239