Today we'll look at a Spark sample application that demonstrates how to send SMS alerts to your system administrators when something goes wrong on your server. We'll do a deep dive on all of the plumbing so you can see how easy it'll be to add notifications to your own application.
Let's get started!
Click the button below to move to the next step of the tutorial.
Here we create a JSON list of administrators who should be notified if a server error occurs.
The only essential piece of data we need is a
phone_number for each administrator.
Next, let's take a look at configuring the Twilio REST Client.
To send a message we'll need to initialize the
TwilioRestClient defined in the Twilio Java Helper Library. It requires reading a
TWILIO_AUTH_TOKEN from environment variables.
The values for your account SID and Auth Token will be in the Twilio console:
Click the eyeball to show your Auth Token to copy and paste.
Next up, we will look at how to handle application exceptions.
Now let's look at how to create a custom message for our notifications.
Here we create an alert message to send out via text message.
You might also decide to include a picture with your alert message and make it an MMS. Perhaps a screenshot of the application when the crash happened? An 'Everything is Okay' meme image?
Let's move forward and look at loading the list of administrators.
And next, we will see how to send a text message.
And that's a wrap!
We've just implemented an automated server notification system for Java and Spark that can push server alerts if anything goes wrong.
Let's see what else you can do with the Twilio Java SDK, next.
We love Java here at Twilio. Here're just a couple more examples of using our Java SDK to easily add some features in your application:
How well is your team doing when interacting with customers? Instantly collect structured data from your users with a survey conducted over a voice call or SMS.
A simple tutorial to prevent no-shows at your business - send your customers automatic appointment notifications.
Thanks for building with us today. Tweet us a comment @twilio to let us know what you think and what you're building!