Skip to content

Commit 2ee3f81

Browse files
committed
Call createUserForUserId before registering the token
1 parent 12af53d commit 2ee3f81

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -412,31 +412,10 @@ public void setUserId(String userId) {
412412
onLogOut();
413413
_email = null;
414414
_userId = userId;
415-
createUserForUserId();
416415
storeEmailAndUserId();
417416
onLogIn();
418417
}
419418

420-
/**
421-
* Creates a user profile for a userId if it does not yet exist.
422-
*/
423-
public void createUserForUserId() {
424-
if (!checkSDKInitialization() || _userId == null) {
425-
return;
426-
}
427-
428-
JSONObject requestJSON = new JSONObject();
429-
try {
430-
addEmailOrUserIdToJson(requestJSON);
431-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
432-
433-
sendPostRequest(IterableConstants.ENDPOINT_CREATE_USERID, requestJSON);
434-
}
435-
catch (JSONException e) {
436-
e.printStackTrace();
437-
}
438-
}
439-
440419
/**
441420
* Tracks a click on the uri if it is an iterable link.
442421
* @param uri the
@@ -548,7 +527,21 @@ public void registerDeviceToken(final String applicationName, final String token
548527
if (token != null) {
549528
new Thread(new Runnable() {
550529
public void run() {
551-
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
530+
if (getUserId() != null) {
531+
createUserForUserId(new IterableHelper.SuccessHandler() {
532+
@Override
533+
public void onSuccess(JSONObject data) {
534+
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
535+
}
536+
}, new IterableHelper.FailureHandler() {
537+
@Override
538+
public void onFailure(String reason, JSONObject data) {
539+
IterableLogger.e(TAG, "Could not create user: " + reason);
540+
}
541+
});
542+
} else {
543+
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
544+
}
552545
}
553546
}).start();
554547
}
@@ -1399,6 +1392,26 @@ private void handleDDL(JSONObject response) {
13991392
setDDLChecked(true);
14001393
}
14011394

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

0 commit comments

Comments
 (0)