Send a message

Note:

Before you can get started, you need to do the following:

  • Set all Conversation API configuration settings.
  • JDK 8 or later and a familiarity with how to create a new Java application.
  • Gradle and a familiarity with how use the Gradle build tools.

Using the Conversation API, you can send messages to any channel you have configured. This tutorial shows you how to set up and send a message in a Java application using the Sinch Java SDK.

Steps:
  1. Set up your Java application
  2. Send your first message

Set up your Java application

  1. Create a new folder where you want to keep your app project. Then, open a terminal or command prompt to that location.
  2. Create a new Java application using Gradle with the following command:
    Copy
    Copied
    gradle init

    In the prompts, select that you want to create an application, name your project and source package app, and then accept the defaults for the rest of the options.

Add the Sinch Java SDK

  1. In your project folder, navigate to the /app folder and open the build.gradle file.
  2. In the dependencies section, add the following line:
    Copy
    Copied
    implementation "com.sinch.sdk:java-sdk:+"
    This adds the Sinch Java SDK to your project.
  3. Save and close the file.

Modify your application

  1. Open the App.java file in your project folder, located in \app\scr\main\java\app. Populate that file with the "App.java" code found on this page and save the file. This code sends a text message.
    Note:

    This sample application is configured for the US Region. If your Conversation API app wasn't created in the US region, you must change the Region.US parameter to Region.EU.

App.java

This code sample is to be used with the instructions on this page to build an application that will send a message.

package app;

// Find your App ID at dashboard.sinch.com/convapi/apps
// Find your Project ID at dashboard.sinch.com/settings/project-management
// Get your Access Key and Access Secret at dashboard.sinch.com/settings/access-keys

import com.sinch.sdk.Sinch;
import com.sinch.sdk.api.conversationapi.model.request.message.TextMessageRequest;
import com.sinch.sdk.api.conversationapi.service.Messages;
import com.sinch.sdk.model.Region;
import com.sinch.sdk.model.conversationapi.ChannelIdentities;
import com.sinch.sdk.model.conversationapi.ChannelRecipientIdentity;
import com.sinch.sdk.model.conversationapi.ConversationChannel;
import com.sinch.sdk.model.conversationapi.Recipient;

public class App {
    private static final String ACCESS_KEY = "";
    private static final String ACCESS_SECRET = "";
    private static final String APP_ID = "";
    private static final String PROJECT_ID = "";
    private static final String CHANNEL = "";
    private static final String IDENTITY = "";
    
    public static void main(String[] args) {
        Sinch.init(ACCESS_KEY, ACCESS_SECRET, PROJECT_ID);
    
        TextMessageRequest messageRequest = new TextMessageRequest("Hello from Conversation API and the Sinch Java SDK.")
            .recipient(RecipientFactory.fromIdentity(ConversationChannel.fromValue(CHANNEL), IDENTITY));
    
        Sinch.conversationApi(Region.US)
            .messages(APP_ID)
            .send(messageRequest);
    
        System.exit(0);
    }
}
  1. Assign your values to the following parameters:
ParameterYour value
APP_IDFind your app ID on your Sinch dashboard.
ACCESS_KEYFind your access key on your Sinch dashboard.
ACCESS_SECRETFind your access secret on your Sinch dashboard.
Note: Access secrets are only available during initial key creation.
PROJECT_IDFind your project ID on your Sinch dashboard.
CHANNELThe channel you want to use to send the message. Available channels are configured for the app on your Sinch dashboard. This guide assumes you've started with an SMS channel, but you can use any channel configured for your app:
  • SMS
  • MESSENGER
  • MMS
  • RCS
  • WHATSAPP
  • VIBER
  • VIBERBM
  • INSTAGRAM
  • TELEGRAM
  • KAKAOTALK
  • APPLEBC
  • LINE
  • WECHAT
IDENTITYThe ID of the contact to which you want to send the message.
  1. Save the file.

Send your first message

Now you can execute the code and send your test message. Run the following command:

Copy
Copied
gradle run

You should receive a message in your configured messaging platform.

Next steps

Now that you know how to send a message, next learn how to handle an incoming message.

Additional resources

Read the links below to learn more:

Was this page helpful?

App.java

This code sample is to be used with the instructions on this page to build an application that will send a message.

package app;

// Find your App ID at dashboard.sinch.com/convapi/apps
// Find your Project ID at dashboard.sinch.com/settings/project-management
// Get your Access Key and Access Secret at dashboard.sinch.com/settings/access-keys

import com.sinch.sdk.Sinch;
import com.sinch.sdk.api.conversationapi.model.request.message.TextMessageRequest;
import com.sinch.sdk.api.conversationapi.service.Messages;
import com.sinch.sdk.model.Region;
import com.sinch.sdk.model.conversationapi.ChannelIdentities;
import com.sinch.sdk.model.conversationapi.ChannelRecipientIdentity;
import com.sinch.sdk.model.conversationapi.ConversationChannel;
import com.sinch.sdk.model.conversationapi.Recipient;

public class App {
    private static final String ACCESS_KEY = "";
    private static final String ACCESS_SECRET = "";
    private static final String APP_ID = "";
    private static final String PROJECT_ID = "";
    private static final String CHANNEL = "";
    private static final String IDENTITY = "";
    
    public static void main(String[] args) {
        Sinch.init(ACCESS_KEY, ACCESS_SECRET, PROJECT_ID);
    
        TextMessageRequest messageRequest = new TextMessageRequest("Hello from Conversation API and the Sinch Java SDK.")
            .recipient(RecipientFactory.fromIdentity(ConversationChannel.fromValue(CHANNEL), IDENTITY));
    
        Sinch.conversationApi(Region.US)
            .messages(APP_ID)
            .send(messageRequest);
    
        System.exit(0);
    }
}