Skip to content

Commit

Permalink
Merge pull request #892 from OneSignal/fix/player_create_if_missing_p…
Browse files Browse the repository at this point in the history
…layer_id_on_put

Create new player if missing player_id on put
  • Loading branch information
jkasten2 authored Nov 22, 2019
2 parents 506f292 + a0a9cdc commit 97ac07e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
package com.onesignal;


import android.support.annotation.Nullable;

import org.json.JSONObject;

public class OSSubscriptionState implements Cloneable {
Expand Down Expand Up @@ -64,8 +66,13 @@ void changed(OSPermissionState state) {
setAccepted(state.getEnabled());
}

void setUserId(String id) {
boolean changed = !id.equals(userId);
void setUserId(@Nullable String id) {
boolean changed = false;
if (id == null)
changed = userId != null;
else if (!id.equals(userId))
changed = true;

userId = id;
if (changed)
observable.notifyChange(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,10 @@ void setExternalUserId(final String externalId) throws JSONException {
abstract void setSubscription(boolean enable);

private void handlePlayerDeletedFromServer() {
OneSignal.Log(OneSignal.LOG_LEVEL.WARN, "Creating new player based on missing player_id noted above.");
OneSignal.handleSuccessfulEmailLogout();
resetCurrentState();
updateIdDependents(null);
scheduleSyncToServer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1229,14 +1229,14 @@ public void testChangeAppId_duringRuntime() throws Exception {

@Test
public void testUserDeletedFromServer() throws Exception {
// First cold boot normal
// 1. Open app and register for the first time.
OneSignalInit();
threadAndTaskWait();

int normalCreateFieldCount = ShadowOneSignalRestClient.lastPost.length();
ShadowOneSignalRestClient.lastPost = null;

// Developer deletes user, cold boots apps should resend all fields
// 2. Developer deletes user and cold restarts the app
restartAppAndElapseTimeToNextSession();
ShadowOneSignalRestClient.failNext = true;
ShadowOneSignalRestClient.setNextFailureJSONResponse(new JSONObject() {{
Expand All @@ -1247,21 +1247,24 @@ public void testUserDeletedFromServer() throws Exception {
OneSignalInit();
threadAndTaskWait();

// 3. Assert the SDK handles the error above and make a new create call.
assertPlayerCreatePushAtIndex(4);
// Checking that the number of fields matches exactly to the original create call.
assertEquals(normalCreateFieldCount, ShadowOneSignalRestClient.lastPost.length());


// Developer deletes users again from dashboard while app is running.
ShadowOneSignalRestClient.lastPost = null;
// 4. Developer deletes users again from dashboard while app is running.
ShadowOneSignalRestClient.failNext = true;
ShadowOneSignalRestClient.setNextFailureJSONResponse(new JSONObject() {{
put("errors", new JSONArray() {{
put("No user with this id found");
}});
put("errors", new JSONArray().put("No user with this id found"));
}});
// 5. Make some call the will attempt a player update
OneSignal.sendTag("key1", "value1");
threadAndTaskWait();

assertEquals(normalCreateFieldCount, ShadowOneSignalRestClient.lastPost.length() - 1);
// 6. Assert the SDK handles the error above and make a new create call.
assertPlayerCreatePushAtIndex(6);
// Checking that the number of fields matches original create call, +1 for tags
assertEquals(normalCreateFieldCount + 1, ShadowOneSignalRestClient.lastPost.length());
}

@Test
Expand Down

0 comments on commit 97ac07e

Please sign in to comment.