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.

Steps:

  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", "{}");

    Leanplum.
    defineAction(

          "JSON_CONTENT",

          1,

          aArgs, new ActionCallback() {




             @Override

             public boolean onResponse(final ActionContext context) {

               LeanplumActivityHelper.queueActionUponActive(new PostponableAction() {

                 @Override

                 public void run() {

                   Activity activity = LeanplumActivityHelper.getCurrentActivity();

                   if (activity == null) {

                     return;

                   }

                   

                   try {

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




                     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(

                             activity);

                     alertDialogBuilder

                             .setTitle("My new template")

                             .setMessage(obj.getString("msg"))

                             .setCancelable(false)

                             .setPositiveButton("OK",

                                     new DialogInterface.OnClickListener() {

                                       public void onClick(DialogInterface dialog, int id) {

                                         context.runActionNamed("OK");

                                       }

                                     });

                     AlertDialog alertDialog = alertDialogBuilder.create();

                     if (!activity.isFinishing()) {

                       alertDialog.show();

                     }

    //              

                   } catch (JSONException e) {

                     e.printStackTrace();

                   }

                 }

               });

               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:

Screen_Recording_2020-08-21_at_9.45.45_AM.gif


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