How To Build An SMS Alert System Using Spring Boot In Java

Getting your Trinity Audio player ready...

We have all been flooded with notifications through multiple apps, whether it’s WhatsApp, Instagram, Reddit, or Snapchat. When there are events that are important, we get notified via the in-app pop-ups, mails, and SMS.

Have you ever tried building an alert system whenever you wanted? Well, in this piece, I would try to do the same — build an SMS alert system whenever an API is called.

All we need is Twilio, a customer engagement platform that thousands of platforms used to create a customized experience for their customers.


What You Need Before You Start

Before starting this, you should have a basic understanding of Java, Maven, and Spring Boot. 

For the purpose of this project, I will be using:


Create a Free Account in Twilio

You need to create a free account with Twilio, where you will need to set up your phone number in the subsequent steps. Also, for creating the free account, no credit card is required.

Use this link to create a Twilio account: https://www.twilio.com/try-twilio

Once you have filled in the details and confirmed your verification mail, you need to add and confirm your phone number, where the SMS alert system will be activated.

Once the phone number is confirmed, you should see a welcome page where you need to complete the form with the below required details before clicking the Get Started with Twilio button.

The Twilio Console will look like:

The fields in Account SID and Auth Token should not be shared, or else anyone can have access to your Twilio account. We will be using both these fields in the upcoming steps.

In the same console, there is a button, ‘Get a trial phone number’ that will enable you to get a valid Twilio phone number. This is required to send SMS with Twilio.

Once the valid phone number is generated, it will be there in the Twilio console along with the Account SID and Auth Token.

Setting up the Spring Boot Application

As usual, you can download the blank demo application from Spring Initializer.

Add the Twilio SDK dependency to the pom.xml file of your spring boot project.

<dependency>
   <groupId>com.twilio.sdk</groupId>
   <artifactId>twilio</artifactId>
   <version>9.0.1</version>
</dependency>

Hence, the complete dependencies list in the pom.xml would look like below:

Get the code here

Building the Alert System

For this simple application illustration, I am using a single controller that would take in the alert message that you want to send as the payload. Hence, the message is customizable.

Now, for using Twilio SDK, you have to initialize the credentials of it by using the init() method in the @PostConstruct annotation.

Get the code here

The Account SID, ${twilio.account.sid} and Auth Token,${twilio.auth.token} in the TwilioConfig class will be passed via the @Value annotation from the application.properties file.

#Twilio platform credentials
twilio.account.sid=<your-twilio-account-sid>
twilio.auth.token=<your-twilio-auth-token>

The core functionality of sending SMS messages works when the static method Message.creator(param1, param2, param3) with all the parameters passed. Here, param1 is the phone number of the receiver (your phone number), param2 is the phone number of the sender (Twilio phone number), and param3 is the message to be sent.

The above functionality has been implemented in the sendSMS() method of the AlertServiceImpl class, which implements the AlertService interface class.

Get the code here

As mentioned earlier, the controller class is pretty straight-forward. It calls the method sendSMS() of the AlertService interface class.

Get the code here

Testing the Application

Once the application is started, you can test it via postman with your customized message.

If it is successful, you will receive the message in your phone number.

There you go! You sent your first SMS using Spring boot.


Note: You can get the complete code along with the postman collection from https://github.com/viveknaskar/sms-alert-system.

I absolutely loved working on this article, and I hope you guys do enjoy working on your own messaging system. This is barely scratching the surface. We can do so much more.

Feel free to play around with the code and try sending SMS messages to multiple people or getting the phone number of the receiver from the payload itself.


If this article provided you with value, please support my work — only if you can afford it. You can also connect with me on X. Thank you!