Leomaris Reyes Iโ€™m Leomaris Reyes, Microsoft MVP, Master Coach at Platzi, and software engineer from the Dominican Republic with more than six years of experience in software development. Xamarin Certified Mobile Developer. Founder of Stemelle. I really love learning new things! ๐Ÿ’š๐Ÿ’•

Using Xamarin.Essentials to manage phone calls and SMS

3 min read 946

Using Xamarin Essentials To Manage Phone Calls And SMS

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:

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

๐Ÿ“ฅ SMS:

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

Itโ€™s important to verify your target Android version. To do so, just follow the next steps:Configuration Steps To Setup Your Android Project With Phone Dialer And SMS

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

  1. Go to your Android project
  2. Right-click in the project and go to Options
  3. Go to Android Applications
  4. 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.
Smartphone Number Dialer API

Letโ€™s see the structure to implement:
Code For Phone Dialer And Color Legend Key

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)

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:Smartphone SMS message composer

Now letโ€™s see the graphical structure:
Code For SMS Message And Color Legend Key

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!

Get setup with LogRocket's modern error tracking in minutes:

  1. Visit https://logrocket.com/signup/ to get an app ID.
  2. Install LogRocket via NPM or script tag. LogRocket.init() must be called client-side, not server-side.
  3. $ npm i --save logrocket 

    // Code:

    import LogRocket from 'logrocket';
    Add to your HTML:

    <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script>
    <script>window.LogRocket && window.LogRocket.init('app/id');</script>
  4. (Optional) Install plugins for deeper integrations with your stack:
    • Redux middleware
    • ngrx middleware
    • Vuex plugin
Get started now
Leomaris Reyes Iโ€™m Leomaris Reyes, Microsoft MVP, Master Coach at Platzi, and software engineer from the Dominican Republic with more than six years of experience in software development. Xamarin Certified Mobile Developer. Founder of Stemelle. I really love learning new things! ๐Ÿ’š๐Ÿ’•

Leave a Reply