Skip to content

Commit e90c62d

Browse files
authored
Merge pull request #68 from Iterable/feature/MOB-34-remove-createUserForUserId
[MOB-34] Use 'preferUserId' flag to create a user by userId instead of the old API
2 parents cf968f8 + 688ae76 commit e90c62d

File tree

3 files changed

+9
-40
lines changed

3 files changed

+9
-40
lines changed

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

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -534,22 +534,7 @@ public void run() {
534534
registerDeviceToken(email, userId, applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
535535
}
536536
});
537-
538-
if (getUserId() != null) {
539-
createUserForUserId(new IterableHelper.SuccessHandler() {
540-
@Override
541-
public void onSuccess(JSONObject data) {
542-
registrationThread.start();
543-
}
544-
}, new IterableHelper.FailureHandler() {
545-
@Override
546-
public void onFailure(String reason, JSONObject data) {
547-
IterableLogger.e(TAG, "Could not create user: " + reason);
548-
}
549-
});
550-
} else {
551-
registrationThread.start();
552-
}
537+
registrationThread.start();
553538
}
554539
}
555540

@@ -1191,6 +1176,11 @@ protected void registerDeviceToken(String email, String userId, String applicati
11911176
device.putOpt(IterableConstants.KEY_DATA_FIELDS, dataFields);
11921177
requestJSON.put(IterableConstants.KEY_DEVICE, device);
11931178

1179+
// Create the user by userId if it doesn't exist
1180+
if (email == null && userId != null) {
1181+
requestJSON.put(IterableConstants.KEY_PREFER_USER_ID, true);
1182+
}
1183+
11941184
sendPostRequest(IterableConstants.ENDPOINT_REGISTER_DEVICE_TOKEN, requestJSON);
11951185
} catch (JSONException e) {
11961186
IterableLogger.e(TAG, "registerDeviceToken: exception", e);
@@ -1402,25 +1392,6 @@ private void handleDDL(JSONObject response) {
14021392
setDDLChecked(true);
14031393
}
14041394

1405-
/**
1406-
* Creates a user profile for a userId if it does not yet exist.
1407-
*/
1408-
private void createUserForUserId(IterableHelper.SuccessHandler onSuccess, IterableHelper.FailureHandler onFailure) {
1409-
if (!checkSDKInitialization() || _userId == null) {
1410-
return;
1411-
}
1412-
1413-
JSONObject requestJSON = new JSONObject();
1414-
try {
1415-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
1416-
1417-
sendPostRequest(IterableConstants.ENDPOINT_CREATE_USERID, requestJSON, onSuccess, onFailure);
1418-
}
1419-
catch (JSONException e) {
1420-
e.printStackTrace();
1421-
}
1422-
}
1423-
14241395
//---------------------------------------------------------------------------------------
14251396
//endregion
14261397

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public final class IterableConstants {
2727
public static final String KEY_ITEMS = "items";
2828
public static final String KEY_NEW_EMAIL = "newEmail";
2929
public static final String KEY_PLATFORM = "platform";
30+
public static final String KEY_PREFER_USER_ID = "preferUserId";
3031
public static final String KEY_RECIPIENT_EMAIL = "recipientEmail";
3132
public static final String KEY_SEND_AT = "sendAt";
3233
public static final String KEY_TEMPLATE_ID = "templateId";
@@ -40,7 +41,6 @@ public final class IterableConstants {
4041
public static final String KEY_USER_TEXT = "userText";
4142

4243
//API Endpoint Key Constants
43-
public static final String ENDPOINT_CREATE_USERID = "users/createUserForUserId";
4444
public static final String ENDPOINT_DISABLE_DEVICE = "users/disableDevice";
4545
public static final String ENDPOINT_GET_INAPP_MESSAGES = "inApp/getMessages";
4646
public static final String ENDPOINT_INAPP_CONSUME = "events/inAppConsume";

iterableapi/src/test/java/com/iterable/iterableapi/IterableApiTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,18 @@ public void testPushRegistrationDeviceFields() throws Exception {
243243
@Test
244244
public void testPushRegistrationWithUserId() throws Exception {
245245
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
246-
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
247246

248247
IterableApi.initialize(RuntimeEnvironment.application, "apiKey", new IterableConfig.Builder().setAutoPushRegistration(false).build());
249248
IterableApi.getInstance().setUserId("testUserId");
250249
IterableApi.getInstance().registerDeviceToken("pushIntegration", "token", IterableConstants.MESSAGING_PLATFORM_FIREBASE);
251250
Thread.sleep(1000); // Since the network request is queued from a background thread, we need to wait
252251
Robolectric.flushBackgroundThreadScheduler();
253-
RecordedRequest createUserRequest = server.takeRequest(1, TimeUnit.SECONDS);
254-
assertNotNull(createUserRequest);
255-
assertEquals("/" + IterableConstants.ENDPOINT_CREATE_USERID, createUserRequest.getPath());
256252

257253
RecordedRequest registerDeviceRequest = server.takeRequest(1, TimeUnit.SECONDS);
258254
assertNotNull(registerDeviceRequest);
259255
assertEquals("/" + IterableConstants.ENDPOINT_REGISTER_DEVICE_TOKEN, registerDeviceRequest.getPath());
256+
JSONObject requestJson = new JSONObject(registerDeviceRequest.getBody().readUtf8());
257+
assertEquals(requestJson.getBoolean(IterableConstants.KEY_PREFER_USER_ID), true);
260258
}
261259

262260
}

0 commit comments

Comments
 (0)