Skip to content

Commit

Permalink
Merge pull request #14 from Iterable/feature/ITBL-2397-in-app-consume
Browse files Browse the repository at this point in the history
Feature/itbl 2397 in app consume
  • Loading branch information
davidtruong authored Jul 18, 2017
2 parents bb58cb5 + e8af255 commit 504fd8a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ public class IterableNotificationDataTest extends ApplicationTestCase<Applicatio
public IterableNotificationDataTest() {
super(Application.class);
}

public void testPayloadParams() throws Exception {
int campaignId = 1;
int templateId = 2;
String messageId = "abc123";
IterableNotificationData iterableNotificationData = new IterableNotificationData(campaignId, templateId, messageId);
assertEquals(1, iterableNotificationData.getCampaignId());
assertEquals(2, iterableNotificationData.getTemplateId());
assertEquals("abc123", iterableNotificationData.getMessageId());
assertEquals(false, iterableNotificationData.getIsGhostPush());
}

public void testPayloadString() throws Exception {
String userInfo = "{\"campaignId\": 1,\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ public void execute(String payload) {
String messageId = dialogOptions.optString(IterableConstants.KEY_MESSAGE_ID);

IterableApi.sharedInstance.trackInAppOpen(campaignId, templateId, messageId);
IterableNotificationData trackParams = new IterableNotificationData(campaignId, templateId, messageId);
IterableInAppManager.showNotification(context, message, trackParams, clickCallback);
IterableApi.sharedInstance.inAppConsume(messageId);
IterableInAppManager.showNotification(context, message, messageId, clickCallback);

}
}
Expand Down Expand Up @@ -589,18 +589,14 @@ public void trackInAppOpen(int campaignId, int templateId, String messageId) {

/**
* Tracks an InApp click.
* @param campaignId
* @param templateId
* @param messageId
* @param buttonIndex
*/
public void trackInAppClick(int campaignId, int templateId, String messageId, int buttonIndex) {
public void trackInAppClick(String messageId, int buttonIndex) {
JSONObject requestJSON = new JSONObject();

try {
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_CAMPAIGN_ID, campaignId);
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
requestJSON.put(IterableConstants.KEY_MESSAGE_ID, messageId);
requestJSON.put(IterableConstants.ITERABLE_IN_APP_BUTTON_INDEX, buttonIndex);
}
Expand All @@ -611,6 +607,24 @@ public void trackInAppClick(int campaignId, int templateId, String messageId, in
sendPostRequest(IterableConstants.ENDPOINT_TRACK_INAPP_CLICK, requestJSON);
}

/**
* Consumes an InApp message.
* @param messageId
*/
public void inAppConsume(String messageId) {
JSONObject requestJSON = new JSONObject();

try {
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_MESSAGE_ID, messageId);
}
catch (JSONException e) {
e.printStackTrace();
}

sendPostRequest(IterableConstants.ENDPOINT_INAPP_CONSUME, requestJSON);
}

//---------------------------------------------------------------------------------------
//endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class IterableConstants {
//API Endpoint Key Constants
public static final String ENDPOINT_DISABLE_DEVICE = "users/disableDevice";
public static final String ENDPOINT_GET_INAPP_MESSAGES = "inApp/getMessages";
public static final String ENDPOINT_INAPP_CONSUME = "events/inAppConsume";
public static final String ENDPOINT_PUSH_TARGET = "push/target";
public static final String ENDPOINT_REGISTER_DEVICE_TOKEN = "users/registerDeviceToken";
public static final String ENDPOINT_TRACK = "events/track";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public class IterableInAppActionListener implements View.OnClickListener {
int index;
String actionName;
IterableHelper.IterableActionHandler onClickCallback;
IterableNotificationData trackParams;
String messageId;

/**
* A custom onClickListener which stores data about the dialog context.
* @param dialog
* @param index
* @param actionName
* @param trackParams
* @param messageId
* @param onClickCallback
*/
public IterableInAppActionListener(Dialog dialog, int index, String actionName, IterableNotificationData trackParams, IterableHelper.IterableActionHandler onClickCallback){
public IterableInAppActionListener(Dialog dialog, int index, String actionName, String messageId, IterableHelper.IterableActionHandler onClickCallback){
this.index = index;
this.actionName = actionName;
this.onClickCallback = onClickCallback;
this.dialog = dialog;
this.trackParams = trackParams;
this.messageId = messageId;
}

/**
Expand All @@ -37,8 +37,8 @@ public IterableInAppActionListener(Dialog dialog, int index, String actionName,
*/
@Override
public void onClick(View v) {
if (trackParams != null) {
IterableApi.sharedInstance.trackInAppClick(trackParams.getCampaignId(), trackParams.getTemplateId(), trackParams.getMessageId(), index);
if (messageId != null) {
IterableApi.sharedInstance.trackInAppClick(messageId, index);
}
if (onClickCallback != null) {
onClickCallback.execute(actionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public class IterableInAppManager {
* @param dialogOptions
* @param clickCallback
*/
public static void showNotification(Context context, JSONObject dialogOptions, IterableNotificationData trackParams, IterableHelper.IterableActionHandler clickCallback) {
public static void showNotification(Context context, JSONObject dialogOptions, String messageId, IterableHelper.IterableActionHandler clickCallback) {
if(dialogOptions != null) {
String type = dialogOptions.optString(IterableConstants.ITERABLE_IN_APP_TYPE);
if (type.equalsIgnoreCase(IterableConstants.ITERABLE_IN_APP_TYPE_FULL)) {
showFullScreenDialog(context, dialogOptions, trackParams, clickCallback);
showFullScreenDialog(context, dialogOptions, messageId, clickCallback);
} else {
showNotificationDialog(context, dialogOptions, trackParams, clickCallback);
showNotificationDialog(context, dialogOptions, messageId, clickCallback);
}
} else {
IterableLogger.d(TAG, "In-App notification not displayed: showNotification must contain valid dialogOptions");
Expand All @@ -56,7 +56,7 @@ public static void showNotification(Context context, JSONObject dialogOptions, I
* @param dialogParameters
* @param clickCallback
*/
static void showNotificationDialog(Context context, JSONObject dialogParameters, IterableNotificationData trackParams, IterableHelper.IterableActionHandler clickCallback) {
static void showNotificationDialog(Context context, JSONObject dialogParameters, String messageId, IterableHelper.IterableActionHandler clickCallback) {
Dialog dialog = new Dialog(context, android.R.style.Theme_Material_NoActionBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
Expand Down Expand Up @@ -105,7 +105,7 @@ static void showNotificationDialog(Context context, JSONObject dialogParameters,
//Buttons
JSONArray buttonJson = dialogParameters.optJSONArray(IterableConstants.ITERABLE_IN_APP_BUTTONS);
if (buttonJson != null) {
verticalLayout.addView(createButtons(context, dialog, buttonJson, null, clickCallback));
verticalLayout.addView(createButtons(context, dialog, buttonJson, messageId, clickCallback));
}

dialog.setContentView(verticalLayout);
Expand All @@ -118,7 +118,7 @@ static void showNotificationDialog(Context context, JSONObject dialogParameters,
* @param dialogParameters
* @param clickCallback
*/
static void showFullScreenDialog(Context context, JSONObject dialogParameters, IterableNotificationData trackParams, IterableHelper.IterableActionHandler clickCallback) {
static void showFullScreenDialog(Context context, JSONObject dialogParameters, String messageId, IterableHelper.IterableActionHandler clickCallback) {
Dialog dialog = new Dialog(context, android.R.style.Theme_Light);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Expand Down Expand Up @@ -184,7 +184,7 @@ static void showFullScreenDialog(Context context, JSONObject dialogParameters, I
//Buttons
JSONArray buttonJson = dialogParameters.optJSONArray(IterableConstants.ITERABLE_IN_APP_BUTTONS);
if (buttonJson != null) {
View buttons = createButtons(context, dialog, buttonJson, trackParams, clickCallback);
View buttons = createButtons(context, dialog, buttonJson, messageId, clickCallback);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Expand Down Expand Up @@ -223,11 +223,11 @@ public static JSONObject getNextMessageFromPayload(String payload) {
* @param context
* @param dialog
* @param buttons
* @param trackParams
* @param messageId
* @param clickCallback
* @return
*/
private static View createButtons(Context context, Dialog dialog, JSONArray buttons, IterableNotificationData trackParams, IterableHelper.IterableActionHandler clickCallback) {
private static View createButtons(Context context, Dialog dialog, JSONArray buttons, String messageId, IterableHelper.IterableActionHandler clickCallback) {
LinearLayout.LayoutParams equalParamWidth = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
Expand All @@ -244,7 +244,7 @@ private static View createButtons(Context context, Dialog dialog, JSONArray butt
button.setBackgroundColor(getIntColorFromJson(buttonJson, IterableConstants.ITERABLE_IN_APP_BACKGROUND_COLOR, Color.LTGRAY));
String action = buttonJson.optString(IterableConstants.ITERABLE_IN_APP_BUTTON_ACTION);
if (!action.isEmpty()) {
button.setOnClickListener(new IterableInAppActionListener(dialog, i, action, trackParams, clickCallback));
button.setOnClickListener(new IterableInAppActionListener(dialog, i, action, messageId, clickCallback));
}

JSONObject textJson = buttonJson.optJSONObject(IterableConstants.ITERABLE_IN_APP_CONTENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
* Created by davidtruong on 5/23/16.
*/
class IterableNotificationData {
private int campaignId;
private int templateId;
private String messageId;
private boolean isGhostPush;
static final String TAG = "IterableNoticationData";

private int campaignId;
private int templateId;
private String messageId;
private boolean isGhostPush;

/**
* Creates the notification data from a string
Expand All @@ -35,22 +37,10 @@ class IterableNotificationData {
isGhostPush = iterableJson.getBoolean(IterableConstants.IS_GHOST_PUSH);
}
} catch (JSONException e) {
e.printStackTrace();
IterableLogger.e(TAG, e.toString());
}
}

/**
* Creates the notification data
* @param campaignId
* @param templateId
* @param messageId
*/
IterableNotificationData(int campaignId, int templateId, String messageId) {
this.campaignId = campaignId;
this.templateId = templateId;
this.messageId = messageId;
}

/**
* Returns the campaignId
* @return
Expand Down

0 comments on commit 504fd8a

Please sign in to comment.