Traditionally, emails were considered the industry standard for delivering important application notifications. It allowed application users to stay updated with important application events without having to log into it every day. However, emails are generally considered to convey messages of less importance as emails are not checked frequently. Therefore, most people ended up missing out on crucial application notifications.
Hence, developers now focus on utilizing more reachable services to deliver system notifications. One such service is the use of SMS messages. SMS messages are frequently checked and prominent as they appear on your mobile phone. Hence, many mission-critical applications have now adopted the use of SMS to notify their consumers of events, such as - One-Time Password Links, MFA Codes, and even application notifications (Facebook uses SMS to inform users about birthdays).
This article will present three ways developers can implement an SMS-driven notification service using Java.
Sending SMS using Java (Pre-Note)
It is essential to understand that there is no native implementation that Java offers to send SMS notifications. Therefore, integrating third-party services is the only way to send SMS notifications via Java.
Some of these tools are paid, while some offer a free plan. Therefore, you can select a third-party service based on your requirements.
01 - Twilio
Twilio is one of the most commonly used services for SMS messaging across several languages. It offers an API that developers can use to send and receive SMS, MMS, or even WhatsApp messages.
Note that Twilio's SMS pricing will change depending on the country that you are located on, message type, and destination. For example, Twilio offers a pay-as-you-go model in Sri Lanka where all messages sent within Sri Lanka are free and only require a payment per SMS sent to an international number. Its pricing model is included below.
Figure - Twilio Pricing
- Minimal latency: There is little to no latency in sending SMS messages. It is in near real-time, thus ensuring users are notified as soon as possible.
- Good documentation: Twilio offers comprehensive developer documentation that provides step-by-step instructions on setting up Twilio to send SMS messages in various languages.
- Too technical: Twilio requires some experience to interact with the service. Hence, non-technical users might need help interacting with the service.
- Requires paid plan for custom Twilio Phone Number: The free plan only offers a Twilio Phone Number for a US or Canada number. To obtain custom Twillio Phone Numbers, you must upgrade to a paid plan.
However, if the pros outweigh the cons, refer to the guide below to find out how you can utilize Twilio to send SMS in Java.
Setting up Twilio
To start sending SMS-driven notifications using Twilio, you require a Twilio account. First, visit their sign-up page and create a free account. After creating an account and verifying your email and phone number, you should see the output below.
Tutorial: How to Send SMS Using Twilio API & Java
Figure - Confirming email and phone number on Twilio
Hereafter, select "SMS" as the Twilio product, and fill in the remaining questions with your preferred answer. The answers I've provided are illustrated below.
Figure - Twilio Sign-Up Configuration
Hereafter, select "Get Started with Twillio" which will redirect you to your Twilio dashboard.
Figure - Twilio Dashboard
Initially, you will have to create a Twilio Phone Number. A Twilio Phone Number is a virtual phone number used to send and receive SMS. Since we are using the free plan, we are restricted to only a US or Canadian phone number. However, you can upgrade your plan and get a local Twilio phone number.
Click on "Phone Numbers", as shown in the figure above, and select "Buy a Number".
Figure - Purchasing a number.
Hereafter, select "US" as the country and purchase any number. This will be used as your Twilio Phone Number. Ensure that the selected number has SMS Capabilities.
Hereafter, you can install the Twilio library using Maven with the syntax shown below.
Afterward, you can use the syntax shown below to send the SMS message.
02 - MessageBird
Secondly, developers can utilize the service - MessageBird to deliver SMS notifications via a Java application. MessageBird is a developer-centric omnichannel communications channel that offers a messaging service billed on a per-message basis that changes based on the country. You can find a complete breakdown of its pricing here.
- Good documentation: MessageBird offers excellent comprehensive developer documentation that provides step-by-step instructions on setting up, to sending SMS messages in various languages.
- Offers multiple channels: Users can implement several messaging channels, improving notification scalability.
- Too technical: MessageBird requires some experience to interact with the service. Hence, non-technical users might find it challenging to interact with the service.
If added scalability benefits are a decisive factor for you, refer to the guide below to set up and send SMS messages using MessageBird.
Tutorial: How to Send SMS Using MessageBird & Java
Initially, you will need to create a free account on MessageBird. Next, ensure that you have verified your phone number. This is mandatory as it is required to get your free testing credits to try out MessageBird.
Figure - Verifying phone number on MessageBird.
After providing the required information, you will be directed to the MessageBird dashboard, as shown below.
Figure - MessageBird Dashboard
Hereafter, navigate to "SMS" -> "API Getting Started" via the URL depicted below.
Hereafter, follow the Getting Started guide and provide your phone number to obtain the free credits.
Figure - Configuring MessageBird
Using MessageBird API in Java
Hereafter, you can add the code required to send the SMS using MessageBird in Java. To do so, first install the MessageBird API using Maven. The Maven Dependency is illustrated below.
Hereafter, you can use the code snippet shown below to send an SMS message using MessageBird.
Use the code above and replace it with your Test Access Key and your verified phone number (since it is in the free plan). After executing this code, you will receive an SMS message.
03 - Courier
Finally, developers can utilize Courier - a multi-channel notification service that simplifies product notifications. Unlike the first two, Courier offers a similar API to a wide variety of notification channels such as - Slack, Email, WhatsApp, and even popular SMS services such as Twilio!
- Efficient UI: Its user interface makes it easy for non-technical people to interact with Courier and deliver product notifications.
- Excellent documentation: Courier offers good documentation, everything from setting up to delivering product notifications.
- Offers analytics: It allows users to track when they viewed and opened the message.
- Can be costly: It can be expensive to consume multiple notification channels.
- Enforced notification count: You are bounded by the maximum monthly notification count (based on the provider).
However, if having analytics and a good UI is a decision-maker for you, refer to the guide below to integrate Courier into a Java application to deliver SMS messages.
Tutorial: How to Send SMS Using Courier & Java
Setting up Courier
Initially, navigate to their sign-up page and create a free account. After creating a free account, you will be guided to your account dashboard, as depicted below.
Figure - Courier Dashboard
Switch to your Test workspace to proceed. Since Courier is a multi-channel notification service, it acts as a middleware for various notification services. Hence, to implement an SMS notification service using Courier, we must set up integrations with third-party notification services such as Twilio or MessageBird. However, the main advantage of using Courier rather than the standalone service is that Courier offers the same API for all services. Hence it is straightforward to change your notification service in the future.
Configuring Twilio with Courier
To configure Twilio with Courier, head over to "Channels". You will see the output shown below.
Figure - Viewing channels offered by Courier
Hereafter, navigate to the "SMS" section and:
- Click on Twilio
- Fill in the information below by referring to your Twilio Account.
Figure - Configuring Twilio on Courier
Afterward, click "Install Provider". If the provider is installed correctly, it will appear in the "Configured Providers" list, as shown below.
Figure - Confirming Twilio Setup
Hereafter, install the Twilio SDK with Maven using the syntax shown below.
Afterward, you will need to create an SMS notification using the Template Notification Designer offered by Courier.
Figure - Configuring the SMS Notification Template
Select "Twilio" from the Configured list shown in the figure above. Afterward, you can start designing your SMS message.
Figure - Creating the template
Finally, publish your messaging template and copy the notification ID.Figure - Copying the notification ID
Hereafter, you can use the syntax shown below to send the SMS message using Courier.
This article proposed three ways to send SMS notifications via Java.
This article helps you select the best SMS notification service for the following Java application. Doing so will help your Java applications easily send event-driven SMS text messages to your customers, allowing them to stay on top of crucial application events!
Thank you for reading.