Receive an SMS Message with Java

Note:

Before you can get started, you need the following already set up:

  • Set all SMS API configuration settings.
  • JDK 21 or later and a familiarity with how to create a new Java application. (The SDK itself only requires JDK 8 or later, but this quickstart guide uses JDK 21 as it is the latest version with long term support.)
  • Apache Maven and a familiarity with how to use the Maven CLI.
  • ngrok. You'll use ngrok to open a tunnel to your local server.

In this tutorial, you will learn how the Java SDK can be used to handle inbound SMS messages. You will create an application that receives an inbound SMS and replies with an SMS acknowledging the content of your inbound message.

Steps:
  1. Set up your Java application
  2. Configure your callback URL
  3. Send your SMS message

Set up your Java application

To quickly get started setting up a simple client application using the Java SDK:

  1. Clone the sinch-sdk-java-quickstart repository. (If you have already cloned the quickstart in another guide, you do not need to do this step.)
  2. Navigate to the getting-started/sms/respond-to-incoming-message/server/src/main/resources folder.
  3. Your Sinch credentials can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required. To add them, open the application.yaml file and enter them under the credentials: tag:
    • project-id:- project ID
    • key-id: - API key ID
    • key-secret: - API key secret
    Note:
    If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.
    You also need to set the SMS region under the sms: tag:
    • region - SMS region (US/EU)

Start your web server and set up a tunnel

When you send an SMS to your Sinch number, the Sinch server sends a request to a callback event URL you need to configure on your SMS service.

  1. Navigate to getting-started/sms/respond-to-incoming-message/server and start the server by executing the following command:
    Copy
    Copied
    mvn clean spring-boot:run
  1. Open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already, install it with the following command:
    Copy
    Copied
    npm install ngrok -g
  2. Open a terminal or command prompt and enter:
    Copy
    Copied
    ngrok http 8090

    You will see a screen like the following. ngrok screenshot

  3. On the highlighed "Forwarding" line, copy the address ending in .ngrok.io and add /SmsEvent to the end of it.

Configure your Callback URL

  1. To configure a callback URL for your Sinch account, login to your dashboard.
  2. Click on the service plan ID link and edit the Callback URL field with the ngrok.io domain URL from the previous step.

Send your SMS message

Now send an SMS message to your Sinch number from your mobile phone and you will get an automatic reply.

Next Steps

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.