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

Clickatell One API: Send personalized SMS reminders in a batch

See more Clickatell Examples

Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.

Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.

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);

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

    String firstPhone = "FIRST_PHONE_NUMBER";
    String secondPhone = "SECOND_PHONE_NUMBER";
    CkJsonObject body = new CkJsonObject();
    body.UpdateString("messages[0].channel","sms");
    body.UpdateString("messages[0].to",firstPhone);
    body.UpdateString("messages[0].content","Your appointment with Lee is at 10:00.");
    body.UpdateString("messages[0].clientMessageId","appt-1042");
    body.UpdateString("messages[1].channel","sms");
    body.UpdateString("messages[1].to",secondPhone);
    body.UpdateString("messages[1].content","Your appointment with Sam is at 14:30.");
    body.UpdateString("messages[1].clientMessageId","appt-1043");

    CkHttpResponse resp = new CkHttpResponse();
    boolean sent = http.HttpJson("POST","https://platform.clickatell.com/v1/message",body,"application/json",resp);
    if (resp.get_StatusCode() == 0) {
        Log.i(TAG, "No HTTP response. Check the message report before resending.");
        return;
        }

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

        return;
        }

    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;
            }

        }

    //  Look up each submitted appointment in the reply using its unique recipient.
    //  This works even if the result order changes or clientMessageId is absent.
    CkJsonObject results = new CkJsonObject();
    int i = 0;
    while ((i < 2)) {
        body.put_I(i);
        String appointment = body.stringOf("messages[i].clientMessageId");
        String phone = body.stringOf("messages[i].to");
        CkJsonObject result = new CkJsonObject();
        boolean found = reply.FindRecord2("messages","to",phone,true,result);
        results.put_I(i);
        results.UpdateString("appointments[i].clientMessageId",appointment);
        if ((found != true) or (result.JsonTypeOf("accepted") != 5)) {
            results.UpdateString("appointments[i].outcome","unknown");
            Log.i(TAG, appointment + " has no confirmed result.");
            }
        else {
            if (result.BoolOf("accepted") == true) {
                results.UpdateString("appointments[i].outcome","accepted");
                if (result.JsonTypeOf("apiMessageId") == 1) {
                    results.UpdateString("appointments[i].apiMessageId",result.stringOf("apiMessageId"));
                    }

                Log.i(TAG, appointment + " accepted.");
                }
            else {
                results.UpdateString("appointments[i].outcome","rejected");
                results.UpdateString("appointments[i].reason",result.stringOf("error.description"));
                Log.i(TAG, appointment + " rejected: " + result.stringOf("error.description"));
                }

            }

        i = i + 1;
        }

    //  This demo writes a snapshot. In an application, update the two appointment records.
    boolean saved = results.WriteFile("appointment-send-results.json");
    if (saved != true) {
        Log.i(TAG, "Could not save the results. Reconcile the submitted messages before retrying.");
        return;
        }

    Log.i(TAG, "Saved appointment-send-results.json");

  }

  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."
  }
}