SMS and MMS Marketing Notifications with Java and Servlets
Ready to implement SMS and MMS marketing notifications with Java and Servlets?
Here's how it'll work at a high level:
- A possible customer sends an SMS to a Twilio phone number you advertise somewhere.
- Your application confirms that the user wants to receive SMS and MMS notifications from your company.
- An administrator or marketing campaign manager uses a web form to craft a message to push to all subscribers via SMS/MMS message.
To get this done, you'll be working with the following tools:
- TwiML and the <Message> verb: We'll use TwiML to manage interactions initiated by the user via SMS.
- Messages Resource: We will use the REST API to broadcast messages out to all subscribers.
Let's get started! Click the button below to move on to the next step of the tutorial.
The Subscriber Model
Before send out marketing notifications to a subscriber, we need to start with the perfect model:
phoneNumberstores where to send the notifications.
subscribedstores if a subscriber is active (only an active subscriber will receive notifications).
Let's see how to handle incoming messages.
Handle Incoming Messages
This is the endpoint that will be called every time our application receives a message.
We begin by getting the user's phone number from the incoming Twilio request. Next, we try to find a
Subscriber model with that phone number.
If there's no subscriber with this phone number, we create one, save it, and respond with a friendly message asking our newest user to send an SMS with "add" as a command. This is done to confirm that they want to receive messages from us.
Twilio can send your web application an HTTP request when certain events happen, such as an incoming text message to one of your Twilio phone numbers. These requests are called webhooks, or status callbacks. For more, check out our guide to Getting Started with Twilio Webhooks. Find other webhook pages, such as a security guide and an FAQ in the Webhooks section of the docs.
We've created a
Subscriber model to keep track of the people that want our messages. Now, let's look at the options we provide our users for opting in or out.
Managing User Subscriptions
We want to provide the user with two SMS commands to manage their subscription status:
These commands will toggle a boolean flag for their
Subscriber record in the database and will determine whether they get our marketing messages. Because we want to respect our user's preferences, we don't opt them in automatically - rather, we have them confirm that they want to receive SMSes or MMSes from us.
To make this happen, we will need to update the controller logic which handles the incoming text message to do a couple things:
- If it is a
removecommand, create/update a
Subscriptionwith the right status in the database.
- If it is a command we don't recognize, send a message explaining the two available commands.
Let's now take a look at how to send notifications.
Send Notifications Out
When a form is submitted, we first grab the message text and/or image URL. Second, we loop through all Subscribers and call the method
send on our
Sender domain object.
When the messages are on their way, render the index page with a success message and delight the marketing team.
Let's take a closer look at how to send SMS or MMS notifications.
Sending SMS or MMS Notifications
When the model object is loaded, it creates a Twilio REST API client that can be used to send SMS and MMS messages. The client requires your Twilio account credentials (an account SID and auth token), which can be found in the console:
Next, all we need to do is
create a message using the
MessageCreator class. The Twilio Message API call creator requires
MediaUrl is an optional parameter.
That's it! We've just implemented a an opt-in process and an administrative interface to run an SMS and MMS marketing campaign. Now all you need is killer content to share with your users via text or MMS.
Where to Next?
Love Java? So do we at Twilio! Here are two other excellent tutorials you should check out:
SMS notifications are a great way to alert humans quickly when important events happen. In this example, we'll show you how to send SMS notifications to a list of people when an error happens in a web application.
Don't let valuable insights slip away - survey your customers with Voice and SMS.
Did this help?
Thanks for checking out this tutorial... tweet @twilio to let us know how you did and what you're building!
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.