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:

We made a custom demo for .
No really. Click here to check it out.

📞 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: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)
 {
            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: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!

: Full visibility into your web apps

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

.
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