Skip to content

Commit

Permalink
Merge pull request #7 from Iterable/feature/SI-92-send-via-sdk
Browse files Browse the repository at this point in the history
Feature/si 92 send via sdk
  • Loading branch information
davidtruong authored Oct 14, 2016
2 parents 40d6f2f + 3a395dd commit 3b8c8aa
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 51 deletions.
154 changes: 105 additions & 49 deletions iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class IterableApi {
private Context _applicationContext;
private String _apiKey;
private String _email;
private String _userId;
private boolean _debugMode;
private Bundle _payloadData;
private IterableNotificationData _notificationData;
Expand All @@ -39,9 +40,6 @@ public class IterableApi {
IterableApi(){
}

IterableApi(Context context, String apiKey, String email){
updateData(context, apiKey, email);
}
//---------------------------------------------------------------------------------------
//endregion

Expand Down Expand Up @@ -104,6 +102,60 @@ void setNotificationData(IterableNotificationData data) {
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* @param currentActivity The current activity
* @param userId The current userId
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKeyWithUserId(Activity currentActivity, String apiKey,
String userId)
{
return sharedInstanceWithApiKeyWithUserId(currentActivity, apiKey, userId, false);
}

/**
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* Allows the IterableApi to be intialized with debugging enabled
* @param currentActivity The current activity
* @param userId
* The current userId@return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKeyWithUserId(Activity currentActivity, String apiKey,
String userId, boolean debugMode)
{
return sharedInstanceWithApiKeyWithUserId((Context) currentActivity, apiKey, userId, debugMode);
}

/**
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* @param currentContext The current context
* @param userId The current userId
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKeyWithUserId(Context currentContext, String apiKey,
String userId)
{
return sharedInstanceWithApiKey(currentContext, apiKey, null, userId, false);
}

/**
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* Allows the IterableApi to be intialized with debugging enabled
* @param currentContext The current context
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKeyWithUserId(Context currentContext, String apiKey,
String userId, boolean debugMode)
{
return sharedInstanceWithApiKey(currentContext, apiKey, null, userId, debugMode);
}

/**
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* @param currentActivity The current activity
* @param email The current email
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, String apiKey,
Expand All @@ -117,6 +169,7 @@ public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, Str
* Should be called whenever the app is opened.
* Allows the IterableApi to be intialized with debugging enabled
* @param currentActivity The current activity
* @param email The current email
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, String apiKey,
Expand All @@ -128,26 +181,34 @@ public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, Str
/**
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* @param currentActivity The current activity
* @param currentContext The current context
* @param email The current email
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKey(Context currentActivity, String apiKey,
public static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
String email)
{
return sharedInstanceWithApiKey(currentActivity, apiKey, email, false);
return sharedInstanceWithApiKey(currentContext, apiKey, email, false);
}

/**
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
* Should be called whenever the app is opened.
* Allows the IterableApi to be intialized with debugging enabled
* @param currentContext The current context
* @param email The current email
* @return stored instance of IterableApi
*/
public static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
String email, boolean debugMode)
{
sharedInstance.updateData(currentContext.getApplicationContext(), apiKey, email);
return sharedInstanceWithApiKey(currentContext, apiKey, email, null, debugMode);
}

private static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
String email, String userId, boolean debugMode)
{
sharedInstance.updateData(currentContext.getApplicationContext(), apiKey, email, userId);

if (currentContext instanceof Activity) {
Activity currentActivity = (Activity) currentContext;
Expand Down Expand Up @@ -217,7 +278,7 @@ public void track(String eventName, String campaignId, String templateId) {
public void track(String eventName, String campaignId, String templateId, JSONObject dataFields) {
JSONObject requestJSON = new JSONObject();
try {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_EVENTNAME, eventName);

requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
Expand All @@ -231,32 +292,9 @@ public void track(String eventName, String campaignId, String templateId, JSONOb
sendRequest(IterableConstants.ENDPOINT_TRACK, requestJSON);
}

public void trackConversion(int campaignId, int templateId) {
trackConversion(campaignId, templateId, null);
}

public void trackConversion(int campaignId, int templateId, JSONObject dataFields) {

JSONObject requestJSON = new JSONObject();

try {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
if (dataFields != null) {
requestJSON.put(IterableConstants.KEY_DATAFIELDS, dataFields);
}
}
catch (JSONException e) {
e.printStackTrace();
}

sendRequest(IterableConstants.ENDPOINT_TRACKCONVERSION, requestJSON);
}

public void sendPush(String email, int campaignId) {
sendPush(email, campaignId, null, null);
}
sendPush(email, campaignId, null, null);
}

/**
* Sends a push campaign to an email address at the given time.
Expand Down Expand Up @@ -300,26 +338,31 @@ public void sendPush(String email, int campaignId, Date sendAt, JSONObject dataF
}

public void updateEmail(String newEmail) {
JSONObject requestJSON = new JSONObject();
if (_email != null) {
JSONObject requestJSON = new JSONObject();

try {
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
}
catch (JSONException e) {
e.printStackTrace();
}
try {
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
}
catch (JSONException e) {
e.printStackTrace();
}

sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);
sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);

_email = newEmail;
_email = newEmail;
} else {
IterableLogger.w(TAG, "updateEmail should not be called with a userId. " +
"Init SDK with sharedInstanceWithApiKey instead of sharedInstanceWithApiKeyWithUserId");
}
}

public void updateUser(JSONObject dataFields) {
JSONObject requestJSON = new JSONObject();

try {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_DATAFIELDS, dataFields);
}
catch (JSONException e) {
Expand Down Expand Up @@ -373,7 +416,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
JSONObject requestJSON = new JSONObject();

try {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
addEmailOrUserIdToJson(requestJSON);
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
requestJSON.put(IterableConstants.KEY_MESSAGE_ID, messageId);
Expand All @@ -392,8 +435,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
protected void disablePush(String token) {
JSONObject requestJSON = new JSONObject();
try {
requestJSON.put(IterableConstants.KEY_TOKEN, token);
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
requestJSON.put(IterableConstants.KEY_TOKEN, token);addEmailOrUserIdToJson(requestJSON);
}
catch (JSONException e) {
e.printStackTrace();
Expand All @@ -406,10 +448,11 @@ protected void disablePush(String token) {

//region Private Fuctions
//---------------------------------------------------------------------------------------
private void updateData(Context context, String apiKey, String email) {
private void updateData(Context context, String apiKey, String email, String userId) {
this._applicationContext = context;
this._apiKey = apiKey;
this._email = email;
this._userId = userId;
}

private void tryTrackNotifOpen(Intent calledIntent) {
Expand All @@ -434,7 +477,8 @@ private void registerDeviceToken(String applicationName, String token, JSONObjec

JSONObject requestJSON = new JSONObject();
try {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
addEmailOrUserIdToJson(requestJSON);

if (dataFields == null) {
dataFields = new JSONObject();
}
Expand Down Expand Up @@ -463,6 +507,18 @@ private void sendRequest(String resourcePath, JSONObject json) {
new IterableRequest().execute(request);
}

private void addEmailOrUserIdToJson(JSONObject requestJSON) {
try {
if (_email != null) {
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
} else {
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public final class IterableConstants {
public static final String KEY_RECIPIENT_EMAIL = "recipientEmail";
public static final String KEY_SEND_AT = "sendAt";
public static final String KEY_TEMPLATE_ID = "templateId";
public static final String KEY_MESSAGE_ID = "messageId";
public static final String KEY_MESSAGE_ID = "messageId";
public static final String KEY_TOKEN = "token";
public static final String KEY_USER_ID = "userId";
public static final String KEY_PLATFORM = "platform";
public static final String KEY_APPLICATIONNAME = "applicationName";
public static final String KEY_DEVICE = "device";
Expand All @@ -33,7 +34,6 @@ public final class IterableConstants {
public static final String ENDPOINT_PUSHTARGET = "push/target";
public static final String ENDPOINT_REGISTERDEVICETOKEN = "users/registerDeviceToken";
public static final String ENDPOINT_TRACK = "events/track";
public static final String ENDPOINT_TRACKCONVERSION = "events/trackConversion";
public static final String ENDPOINT_TRACKPURCHASE = "commerce/trackPurchase";
public static final String ENDPOINT_TRACKPUSHOPEN = "events/trackPushOpen";
public static final String ENDPOINT_UPDATEEMAIL = "users/updateEmail";
Expand Down

0 comments on commit 3b8c8aa

Please sign in to comment.