Android™ Requires Chilkat v11.0.0+
Android™
SendGrid: Schedule a reminder and cancel it by batch ID
See more SendGrid Examples
Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.
Create a batch ID ยท Cancel a batch
Chilkat Android™ Downloads
// 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.
success = false;
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.put_AuthToken("SENDGRID_API_KEY");
CkHttpResponse resp = new CkHttpResponse();
// On cancellation, set this to the batch ID saved with the original appointment.
// This branch does not create a new batch or send another reminder.
String batchToCancel = "";
if (batchToCancel != "") {
CkJsonObject cancellation = new CkJsonObject();
cancellation.UpdateString("batch_id",batchToCancel);
cancellation.UpdateString("status","cancel");
success = http.HttpJson("POST","https://api.sendgrid.com/v3/user/scheduled_sends",cancellation,"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 201) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
Log.i(TAG, "Batch cancellation accepted.");
return;
}
success = http.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 201) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
CkJsonObject batch = new CkJsonObject();
success = batch.Load(resp.bodyStr());
if (success != true) {
Log.i(TAG, "Unexpected response: expected JSON.");
return;
}
String batchId = batch.stringOf("batch_id");
if (batch.HasMember("batch_id") != true) {
Log.i(TAG, "Missing batch_id in response.");
return;
}
// Allocate one batch per independently cancelable appointment.
Log.i(TAG, "Save this batch ID with the appointment: " + batchId);
CkDateTime sendTime = new CkDateTime();
sendTime.SetFromCurrentSystemTime();
sendTime.AddSeconds(3600);
CkJsonObject message = new CkJsonObject();
message.UpdateString("batch_id",batchId);
message.UpdateInt("send_at",sendTime.GetAsUnixTime(false));
message.UpdateString("from.email","sender@example.com");
message.UpdateString("personalizations[0].to[0].email","recipient@example.com");
message.UpdateString("subject","Appointment reminder");
message.UpdateString("content[0].type","text/plain");
message.UpdateString("content[0].value","Your appointment is tomorrow at 9:30 AM Central.");
success = http.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",message,"application/json",resp);
if (success != true) {
if (resp.get_StatusCode() == 0) {
Log.i(TAG, http.lastErrorText());
}
else {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
}
return;
}
if (resp.get_StatusCode() != 202) {
Log.i(TAG, "HTTP " + String.valueOf(resp.get_StatusCode()) + ": " + resp.bodyStr());
return;
}
Log.i(TAG, "Reminder scheduled for " + sendTime.getAsTimestamp(false));
}
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."
}
}