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.
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.
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.
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:
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.
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! ๐
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 theComposeAsync
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!
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Would you be interested in joining LogRocket's developer community?
Join LogRocketโs Content Advisory Board. Youโll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowToast notifications are messages that appear on the screen to provide feedback to users. When users interact with the user […]
Deno’s features and built-in TypeScript support make it appealing for developers seeking a secure and streamlined development experience.
It can be difficult to choose between types and interfaces in TypeScript, but in this post, you’ll learn which to use in specific use cases.
This tutorial demonstrates how to build, integrate, and customize a bottom navigation bar in a Flutter app.