Skip to content

Commit 335700e

Browse files
authored
Merge pull request #22 from Iterable/feature/ITBL-3398-updateSubscriptions
Feature/itbl 3398 update subscriptions
2 parents 8d9a06c + 9031955 commit 335700e

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
1313
import com.google.android.gms.common.GooglePlayServicesRepairableException;
1414

15+
import org.json.JSONArray;
1516
import org.json.JSONException;
1617
import org.json.JSONObject;
1718

1819
import java.io.IOException;
1920
import java.text.SimpleDateFormat;
21+
import java.util.Arrays;
2022
import java.util.Date;
2123
import java.util.TimeZone;
2224
import java.util.regex.Matcher;
@@ -501,7 +503,6 @@ public void registerForPush(String iterableAppId, String projectNumber, String p
501503

502504
/**
503505
* Disables the device from push notifications
504-
*
505506
* @param iterableAppId
506507
* @param gcmProjectNumber
507508
*/
@@ -511,7 +512,6 @@ public void disablePush(String iterableAppId, String gcmProjectNumber) {
511512

512513
/**
513514
* Disables the device from push notifications
514-
*
515515
* @param iterableAppId
516516
* @param projectNumber
517517
* @param pushServicePlatform
@@ -521,6 +521,40 @@ public void disablePush(String iterableAppId, String projectNumber, String pushS
521521
new IterablePushRegistration().execute(data);
522522
}
523523

524+
/**
525+
* Updates the user subscription preferences
526+
* @param emailListIds
527+
* @param unsubscribedChannelIds
528+
* @param unsubscribedMessageTypeIds
529+
* @discussion passing in an empty array will clear subscription list, passing in null will not modify the list
530+
*/
531+
public void updateSubscriptions(Integer[] emailListIds, Integer[] unsubscribedChannelIds, Integer[] unsubscribedMessageTypeIds) {
532+
JSONObject requestJSON = new JSONObject();
533+
addEmailOrUserIdToJson(requestJSON);
534+
535+
tryAddArrayToJSON(requestJSON, IterableConstants.KEY_EMAIL_LIST_IDS, emailListIds);
536+
tryAddArrayToJSON(requestJSON, IterableConstants.KEY_UNSUB_CHANNEL, unsubscribedChannelIds);
537+
tryAddArrayToJSON(requestJSON, IterableConstants.KEY_UNSUB_MESSAGE, unsubscribedMessageTypeIds);
538+
539+
sendPostRequest(IterableConstants.ENDPOINT_UPDATE_USER_SUBS, requestJSON);
540+
}
541+
542+
/**
543+
* Attempts to add an array as a JSONArray to a JSONObject
544+
* @param requestJSON
545+
* @param key
546+
* @param value
547+
*/
548+
void tryAddArrayToJSON(JSONObject requestJSON, String key, Object[] value) {
549+
if (requestJSON != null && key != null && value != null)
550+
try {
551+
JSONArray mJSONArray = new JSONArray(Arrays.asList(value));
552+
requestJSON.put(key, mJSONArray);
553+
} catch (JSONException e) {
554+
IterableLogger.e(TAG, e.toString());
555+
}
556+
}
557+
524558
/**
525559
* Gets a notification from Iterable and displays it on device.
526560
* @param context

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public final class IterableConstants {
1717
public static final String KEY_DATA_FIELDS = "dataFields";
1818
public static final String KEY_DEVICE = "device";
1919
public static final String KEY_EMAIL = "email";
20-
public static final String KEY_EVENT_NAME = "eventName";
20+
public static final String KEY_EMAIL_LIST_IDS = "emailListIds";
21+
public static final String KEY_EVENT_NAME = "eventName";
2122
public static final String KEY_ITEMS = "items";
2223
public static final String KEY_NEW_EMAIL = "newEmail";
2324
public static final String KEY_PLATFORM = "platform";
@@ -27,6 +28,8 @@ public final class IterableConstants {
2728
public static final String KEY_MESSAGE_ID = "messageId";
2829
public static final String KEY_TOKEN = "token";
2930
public static final String KEY_TOTAL = "total";
31+
public static final String KEY_UNSUB_CHANNEL = "unsubscribedChannelIds";
32+
public static final String KEY_UNSUB_MESSAGE = "unsubscribedMessageTypeIds";
3033
public static final String KEY_USER_ID = "userId";
3134
public static final String KEY_USER = "user";
3235

@@ -41,8 +44,9 @@ public final class IterableConstants {
4144
public static final String ENDPOINT_TRACK_INAPP_OPEN = "events/trackInAppOpen";
4245
public static final String ENDPOINT_TRACK_PURCHASE = "commerce/trackPurchase";
4346
public static final String ENDPOINT_TRACK_PUSH_OPEN = "events/trackPushOpen";
44-
public static final String ENDPOINT_UPDATE_EMAIL = "users/updateEmail";
4547
public static final String ENDPOINT_UPDATE_USER = "users/update";
48+
public static final String ENDPOINT_UPDATE_EMAIL = "users/updateEmail";
49+
public static final String ENDPOINT_UPDATE_USER_SUBS = "users/updateSubscriptions";
4650

4751
public static final String PUSH_APP_ID = "IterableAppId";
4852
public static final String PUSH_GCM_PROJECT_NUMBER = "GCMProjectNumber";

0 commit comments

Comments
 (0)