Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Clickatell One API: Send an SMS and get its message ID

See more Clickatell Examples

Send one text using an SMS-enabled One API integration. Supply an international phone number starting with the country code, using digits only. For a US two-way integration, set from to its provisioned two-way number. With a test integration, use a registered test phone.

HTTP 202 means the submission was processed. Inspect accepted for each message. Save the returned apiMessageId with the application record and use it to look up delivery status. When a multi-message submission returns HTTP 207, check each result before retrying any rejected message.

Already using the SMS-only REST integration? Its documented request is POST /messages with the integration key in Authorization and a body such as {"content":"Test message","to":["PHONE_NUMBER"]}. One API has a different request body and a separate integration key. SMS API reference

Clickatell API reference

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    //  Supply the key for this One API integration from application configuration.
    http.SetRequestHeader("Authorization","ONE_API_KEY");
    http.SetRequestHeader("Accept","application/json");

    CkJsonObject body = new CkJsonObject();
    body.UpdateString("messages[0].channel","sms");
    body.UpdateString("messages[0].to","PHONE_NUMBER");
    body.UpdateString("messages[0].content","Test message: your order is ready.");
    //  For a two-way sender, supply the number provisioned on the integration.
    String sender = "";
    if (sender != "") {
        body.UpdateString("messages[0].from",sender);
        }

    CkHttpResponse resp = new CkHttpResponse();
    success = http.HttpJson("POST","https://platform.clickatell.com/v1/message",body,"application/json",resp);
    if (resp.get_StatusCode() == 0) {
        Log.i(TAG, "The send outcome is unknown. Check the message report before resending.");
        return;
        }

    CkJsonObject reply = new CkJsonObject();
    boolean parsed = reply.Load(resp.bodyStr());
    if ((success != true) or ((resp.get_StatusCode() != 202) and (resp.get_StatusCode() != 207))) {
        Log.i(TAG, "Clickatell returned HTTP " + String.valueOf(resp.get_StatusCode()));
        if (parsed == true) {
            if (reply.HasMember("error.description") == true) {
                Log.i(TAG, reply.stringOf("error.description"));
                }

            }

        return;
        }

    if (parsed != true) {
        Log.i(TAG, "Expected a JSON response.");
        return;
        }

    //  A request-level error applies to the submission as a whole.
    if (reply.HasMember("error.code") == true) {
        if (reply.IsNullOf("error.code") != true) {
            Log.i(TAG, "Submission error " + reply.stringOf("error.code") + "  " + reply.stringOf("error.description"));
            return;
            }

        }

    int count = reply.SizeOfArray("messages");
    if (count <= 0) {
        Log.i(TAG, "No recipient results returned. Check the message report before resending.");
        return;
        }

    int i = 0;
    while ((i < count)) {
        reply.put_I(i);
        int resultNumber = i + 1;
        //  JsonTypeOf returns 5 for a JSON boolean.
        if (reply.JsonTypeOf("messages[i].accepted") != 5) {
            Log.i(TAG, "Result " + String.valueOf(resultNumber) + " has no acceptance flag.");
            }
        else {
            if (reply.BoolOf("messages[i].accepted") == true) {
                String messageId = reply.stringOf("messages[i].apiMessageId");
                if ((reply.JsonTypeOf("messages[i].apiMessageId") != 1) or (messageId == "")) {
                    Log.i(TAG, "Result " + String.valueOf(resultNumber) + " was accepted without a tracking ID.");
                    }
                else {
                    Log.i(TAG, "Result " + String.valueOf(resultNumber) + " accepted. Message ID: " + messageId);
                    }

                }
            else {
                Log.i(TAG, "Result " + String.valueOf(resultNumber) + " rejected: " + reply.stringOf("messages[i].error.code") + "  " + reply.stringOf("messages[i].error.description"));
                }

            }

        i = i + 1;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}