Skip to content

Commit 44bc987

Browse files
authored
Merge pull request #63 from Iterable/feature/ITBL-6230-call-createUserForUserId
Feature/itbl 6230 call create user for user
2 parents 4baccce + 9cc5919 commit 44bc987

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,27 @@ public void registerDeviceToken(final String applicationName, final String token
525525
}
526526

527527
if (token != null) {
528-
new Thread(new Runnable() {
528+
final Thread registrationThread = new Thread(new Runnable() {
529529
public void run() {
530530
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
531531
}
532-
}).start();
532+
});
533+
534+
if (getUserId() != null) {
535+
createUserForUserId(new IterableHelper.SuccessHandler() {
536+
@Override
537+
public void onSuccess(JSONObject data) {
538+
registrationThread.start();
539+
}
540+
}, new IterableHelper.FailureHandler() {
541+
@Override
542+
public void onFailure(String reason, JSONObject data) {
543+
IterableLogger.e(TAG, "Could not create user: " + reason);
544+
}
545+
});
546+
} else {
547+
registrationThread.start();
548+
}
533549
}
534550
}
535551

@@ -1378,6 +1394,25 @@ private void handleDDL(JSONObject response) {
13781394
setDDLChecked(true);
13791395
}
13801396

1397+
/**
1398+
* Creates a user profile for a userId if it does not yet exist.
1399+
*/
1400+
private void createUserForUserId(IterableHelper.SuccessHandler onSuccess, IterableHelper.FailureHandler onFailure) {
1401+
if (!checkSDKInitialization() || _userId == null) {
1402+
return;
1403+
}
1404+
1405+
JSONObject requestJSON = new JSONObject();
1406+
try {
1407+
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
1408+
1409+
sendPostRequest(IterableConstants.ENDPOINT_CREATE_USERID, requestJSON, onSuccess, onFailure);
1410+
}
1411+
catch (JSONException e) {
1412+
e.printStackTrace();
1413+
}
1414+
}
1415+
13811416
//---------------------------------------------------------------------------------------
13821417
//endregion
13831418

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public final class IterableConstants {
4040
public static final String KEY_USER_TEXT = "userText";
4141

4242
//API Endpoint Key Constants
43+
public static final String ENDPOINT_CREATE_USERID = "users/createUserForUserId";
4344
public static final String ENDPOINT_DISABLE_DEVICE = "users/disableDevice";
4445
public static final String ENDPOINT_GET_INAPP_MESSAGES = "inApp/getMessages";
4546
public static final String ENDPOINT_INAPP_CONSUME = "events/inAppConsume";

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected String doInBackground(IterableApiRequest... params) {
118118
in.close();
119119
requestResult = response.toString();
120120
} catch (IOException e) {
121-
IterableLogger.e(TAG, e.getMessage());
121+
IterableLogger.e(TAG, e.getMessage(), e);
122122
error = e.getMessage();
123123
}
124124

@@ -128,7 +128,7 @@ protected String doInBackground(IterableApiRequest... params) {
128128
try {
129129
jsonResponse = new JSONObject(requestResult);
130130
} catch (Exception e) {
131-
IterableLogger.e(TAG, e.getMessage());
131+
IterableLogger.e(TAG, e.getMessage(), e);
132132
jsonError = e.getMessage();
133133
}
134134

@@ -164,10 +164,10 @@ protected String doInBackground(IterableApiRequest... params) {
164164
handleFailure("Received non-200 response: " + responseCode, jsonResponse);
165165
}
166166
} catch (JSONException e) {
167-
IterableLogger.e(TAG, e.getMessage());
167+
IterableLogger.e(TAG, e.getMessage(), e);
168168
handleFailure(e.getMessage(), null);
169169
} catch (IOException e) {
170-
IterableLogger.e(TAG, e.getMessage());
170+
IterableLogger.e(TAG, e.getMessage(), e);
171171
handleFailure(e.getMessage(), null);
172172
} finally {
173173
if (urlConnection != null) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,23 @@ public void testPushRegistrationDeviceFields() throws Exception {
240240
assertEquals(IterableConstants.ITBL_KEY_SDK_VERSION_NUMBER, dataFields.getString("iterableSdkVersion"));
241241
}
242242

243+
@Test
244+
public void testPushRegistrationWithUserId() throws Exception {
245+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
246+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
247+
248+
IterableApi.initialize(RuntimeEnvironment.application, "apiKey", new IterableConfig.Builder().setAutoPushRegistration(false).build());
249+
IterableApi.getInstance().setUserId("testUserId");
250+
IterableApi.getInstance().registerDeviceToken("pushIntegration", "token", IterableConstants.MESSAGING_PLATFORM_FIREBASE);
251+
Thread.sleep(1000); // Since the network request is queued from a background thread, we need to wait
252+
Robolectric.flushBackgroundThreadScheduler();
253+
RecordedRequest createUserRequest = server.takeRequest(1, TimeUnit.SECONDS);
254+
assertNotNull(createUserRequest);
255+
assertEquals("/" + IterableConstants.ENDPOINT_CREATE_USERID, createUserRequest.getPath());
256+
257+
RecordedRequest registerDeviceRequest = server.takeRequest(1, TimeUnit.SECONDS);
258+
assertNotNull(registerDeviceRequest);
259+
assertEquals("/" + IterableConstants.ENDPOINT_REGISTER_DEVICE_TOKEN, registerDeviceRequest.getPath());
260+
}
261+
243262
}

0 commit comments

Comments
 (0)