Custom template with JSON object [Android]

The following article describes how to create a custom template that receives a JSON string from the Leanplum Dashboard.


  1. Place the following Leanplum action definitions before Leanplum.start() call.

    For our example, we will name our new Action JSON_CONTENT. You can change this name by replacing the value of the NAME property inside the class. Keep in mind that this is the name that will also be displayed in Leanplum Dashboard. Inside the PostponableAction' run method we are parsing the received JSON string to JSON object using JSON object. For more info about the JSONObject class and its methods check out this Android article. Inside the sample code, we will take a single String value with obj.getString("msg") and displaying it in an alert.

    ActionArgs aArgs = new ActionArgs().with("Message", "{}");




          aArgs, new ActionCallback() {


             public boolean onResponse(final ActionContext context) {

               LeanplumActivityHelper.queueActionUponActive(new PostponableAction() {


                 public void run() {

                   Activity activity = LeanplumActivityHelper.getCurrentActivity();

                   if (activity == null) {




                   try {

                     JSONObject obj = new JSONObject(context.stringNamed("Message"));

                     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(



                             .setTitle("My new template")




                                     new DialogInterface.OnClickListener() {

                                       public void onClick(DialogInterface dialog, int id) {




                     AlertDialog alertDialog = alertDialogBuilder.create();

                     if (!activity.isFinishing()) {




                   } catch (JSONException e) {





               return true;






  2. Build the Android application
  3. Register the new template in the Dashboard.
  4. Create a new message in the Dashboard with the new template and set a json string in the message body.  Example:
    Sample JSON
    {"title":"My custom template","msg":"my custom message","okBtn":"ok","cancelBtn":"close", "likes":3, "rating":{"value":4}}

Final result:


Was this article helpful?
Have more questions? Submit a request