June 15, 2021
Using Xamarin.Essentials to manage phone calls and SMS
admin

I am happy to see you here! In this tutorial, we will explore two very important functionalities in Xamarin: how to manage the Phone Dialer and how to send SMS. We will learn how to use them via the APIs that Xamarin.Essentials provides us.

Nowadays, it is useful to integrate phone calls and SMS interactions within our applications to make a product that covers at least the basic functionalities of the user. This way, users can remain in the applications without the need to exit an app in order to send a message or make a phone call.

In the past, we previously had to add two NuGet packages — one for calls and one for SMS — which was complicated and time-consuming. Now, we can create a phone dialer in a much easier way. Let’s start by explaining how to use the phone dialer, and then SMS.

What is Xamarin.Essentials?

Xamarin.Essentials provides a cross-platform developer API for your mobile applications and allows us to develop in the entire Xamarin Forms environment, Android, iOS, or UWP.

Great! So what functionalities/APIs does Xamarin.Essentials offer? There are several, such as SMS, accelerometer, phone dialer, preferences, and many others. If you want to learn about all of them and how to implement each one, I recommend this article, where you will have the feature guide to each functionality.

Setting up Xamarin.Essentials on specific platforms

It’s important to know that Xamarin.Essentials supports a minimum version of Android 4.4, corresponding to API level 19, but the target Android version for build must be 9.0 or 10.0, corresponding to API level 28 and level 29.

If you are up to date, Xamarin.Essentials is already added to your projects, so you don’t need an additional configuration at this time.

If you have an older version and Xamarin.Essentials isn’t working well, you can check to see if everything is correct with your implementation in the docs.

Getting started with Xamarin.Essentials

Platform settings

In some cases, each platform needs an additional setup to make the implementation effective. In this case, the configuration is only needed for Android. Let’s set up all the configurations needed to use the phone dialer and SMS.

First, open the MainActivity.xml file from your Android project, and, inside of the manifest node, add the following code:

Phone dialer:

<queries>
<intent>
<action android:name=”android.intent.action.DIAL” />
<data android:scheme=”tel”/>
</intent>
</queries>

SMS:

<queries>
<intent>
<action android:name=”android.intent.action.VIEW” />
<data android:scheme=”smsto”/>
</intent>
</queries>

It’s important to verify your target Android version. To do so, just follow the next steps:

As the image shows above, please apply the following steps:

Go to your Android project

Right-click in the project and go to Options

Go to Android Applications

Go to Target Android version and set “Android 9.0” (API level 28) as the default

If your target Android version is set to Android 11, you must update your Android manifest with queries that are used with the new package visibility requirements.

Both iOS and UWP have no additional setup required.

Making phone calls with Xamarin.Essentials

To make phone calls, we have a PhoneDialer class, which allows us to open the phone number dialer.

Internally, when we are using this API, it formats the telephone number based on its country of origin for when it shows up on the telephone keypad.

Let’s see the structure to implement:

First, we create the method for calls that can receive the telephone number parameter and has the name PlacePhoneCall. Next, we add the most important class of this explanation, which is PhoneDialer. This class allows us to open the numeric keyboard of our phones.

Finally, with the Open method, we add the telephone number that we want to be displayed when the numeric keyboard is opened. (We already asked for this information in the parent method called PlacePhoneCall.)

Here’s the code example:

public void PlacePhoneCall(string number)
{
PhoneDialer.Open(number);
}

Done! Our phone dialer is ready!

Sending SMS with Xamarin.Essentials

The SMS class allows us to open the message board through the ComposeAsync method, which receives a SmsMessage value as a parameter.

The SMS message receives the body and the recipient(s) as values, like so:

Now let’s see the graphical structure:

In this example, we create the SendSms method in which we can add the class to send the SMS. We will receive the parameters of the SMS text and the recipients.

Within the previously created method, we added the Sms class, which is in charge of opening the message board with the desired text.

Finally, we add the ComposeAsync method, which will receive the message that you want to present. Note that we are using an asynchronous method. If you want to know more information on this topic, check out this article.

Note that the message variable is type SmsMessage, which is responsible for receiving the text of the message. Later, the recipients will be sent in the ComposeAsync method. You will see this example in the code implementation added below.

Here’s a code example:

public async Task SendSms(string messageText, string recipient)
{
var message = new SmsMessage(messageText, new []{ recipient });
await Sms.ComposeAsync(message);
}

If you want to send SMS to more than one user, you can! To do so, you must change the parameter type by a string array:

public async Task SendSms(string messageText, string[] recipients)
{
var message = new SmsMessage(messageText, recipient );
await Sms.ComposeAsync(message);
}

Done! Our SMS is ready! Thanks for reading!

The post Using Xamarin.Essentials to manage phone calls and SMS appeared first on LogRocket Blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send