Skip to content

Commit a52af2a

Browse files
authored
Merge pull request #54 from Iterable/feature/ITBL-5598-auto-push-registration
[ITBL-5598] Disable/re-register device token when email or userId changes
2 parents 7c0e405 + 43d300b commit a52af2a

File tree

3 files changed

+148
-21
lines changed

3 files changed

+148
-21
lines changed

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

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ public static void initialize(Context context, String apiKey, IterableConfig con
219219
}
220220
sharedInstance.sdkCompatEnabled = false;
221221
sharedInstance.retrieveEmailAndUserId();
222+
if (sharedInstance.config.autoPushRegistration && sharedInstance.isInitialized()) {
223+
sharedInstance.registerForPush();
224+
}
222225
}
223226

224227
/**
@@ -367,9 +370,19 @@ private static IterableApi sharedInstanceWithApiKey(Context currentContext, Stri
367370
* @param email User email
368371
*/
369372
public void setEmail(String email) {
373+
if (_email != null && _email.equals(email)) {
374+
return;
375+
}
376+
377+
if (_email == null && _userId == null && email == null) {
378+
return;
379+
}
380+
381+
onLogOut();
370382
_email = email;
371383
_userId = null;
372384
storeEmailAndUserId();
385+
onLogIn();
373386
}
374387

375388
/**
@@ -380,30 +393,19 @@ public void setEmail(String email) {
380393
* @param userId User ID
381394
*/
382395
public void setUserId(String userId) {
383-
_email = null;
384-
_userId = userId;
385-
storeEmailAndUserId();
386-
}
387-
388-
private void storeEmailAndUserId() {
389-
try {
390-
SharedPreferences.Editor editor = getPreferences().edit();
391-
editor.putString(IterableConstants.SHARED_PREFS_EMAIL_KEY, _email);
392-
editor.putString(IterableConstants.SHARED_PREFS_USERID_KEY, _userId);
393-
editor.commit();
394-
} catch (Exception e) {
395-
IterableLogger.e(TAG, "Error while persisting email/userId", e);
396+
if (_userId != null && _userId.equals(userId)) {
397+
return;
396398
}
397-
}
398399

399-
private void retrieveEmailAndUserId() {
400-
try {
401-
SharedPreferences prefs = getPreferences();
402-
_email = prefs.getString(IterableConstants.SHARED_PREFS_EMAIL_KEY, null);
403-
_userId = prefs.getString(IterableConstants.SHARED_PREFS_USERID_KEY, null);
404-
} catch (Exception e) {
405-
IterableLogger.e(TAG, "Error while retrieving email/userId", e);
400+
if (_email == null && _userId == null && userId == null) {
401+
return;
406402
}
403+
404+
onLogOut();
405+
_email = null;
406+
_userId = userId;
407+
storeEmailAndUserId();
408+
onLogIn();
407409
}
408410

409411
/**
@@ -1264,6 +1266,39 @@ private String getAdvertisingId() {
12641266
return advertisingId;
12651267
}
12661268

1269+
private void storeEmailAndUserId() {
1270+
try {
1271+
SharedPreferences.Editor editor = getPreferences().edit();
1272+
editor.putString(IterableConstants.SHARED_PREFS_EMAIL_KEY, _email);
1273+
editor.putString(IterableConstants.SHARED_PREFS_USERID_KEY, _userId);
1274+
editor.commit();
1275+
} catch (Exception e) {
1276+
IterableLogger.e(TAG, "Error while persisting email/userId", e);
1277+
}
1278+
}
1279+
1280+
private void retrieveEmailAndUserId() {
1281+
try {
1282+
SharedPreferences prefs = getPreferences();
1283+
_email = prefs.getString(IterableConstants.SHARED_PREFS_EMAIL_KEY, null);
1284+
_userId = prefs.getString(IterableConstants.SHARED_PREFS_USERID_KEY, null);
1285+
} catch (Exception e) {
1286+
IterableLogger.e(TAG, "Error while retrieving email/userId", e);
1287+
}
1288+
}
1289+
1290+
private void onLogOut() {
1291+
if (config.autoPushRegistration && isInitialized()) {
1292+
disablePush();
1293+
}
1294+
}
1295+
1296+
private void onLogIn() {
1297+
if (config.autoPushRegistration && isInitialized()) {
1298+
registerForPush();
1299+
}
1300+
}
1301+
12671302
//---------------------------------------------------------------------------------------
12681303
//endregion
12691304

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public class IterableConfig {
2121
*/
2222
final IterableCustomActionHandler customActionHandler;
2323

24+
/**
25+
* If set to `true`, the SDK will automatically register the push token when you
26+
* call {@link IterableApi#setUserId(String)} or {@link IterableApi#setEmail(String)}
27+
* and disable the old device entry when the user logs out
28+
*/
29+
final boolean autoPushRegistration;
30+
2431
/**
2532
* GCM sender ID for the previous integration
2633
* Only set this if you're migrating from GCM to FCM and they're in different projects / have different sender IDs
@@ -31,13 +38,15 @@ private IterableConfig(Builder builder) {
3138
pushIntegrationName = builder.pushIntegrationName;
3239
urlHandler = builder.urlHandler;
3340
customActionHandler = builder.customActionHandler;
41+
autoPushRegistration = builder.autoPushRegistration;
3442
legacyGCMSenderId = builder.legacyGCMSenderId;
3543
}
3644

3745
public static class Builder {
3846
private String pushIntegrationName;
3947
private IterableUrlHandler urlHandler;
4048
private IterableCustomActionHandler customActionHandler;
49+
private boolean autoPushRegistration = true;
4150
private String legacyGCMSenderId;
4251

4352
public Builder() {}
@@ -70,6 +79,18 @@ public Builder setCustomActionHandler(IterableCustomActionHandler customActionHa
7079
return this;
7180
}
7281

82+
/**
83+
* Enable or disable automatic push token registration
84+
* If set to `true`, the SDK will automatically register the push token when you
85+
* call {@link IterableApi#setUserId(String)} or {@link IterableApi#setEmail(String)}
86+
* and disable the old device entry when the user logs out
87+
* @param enabled Enable automatic push token registration
88+
*/
89+
public Builder setAutoPushRegistration(boolean enabled) {
90+
this.autoPushRegistration = enabled;
91+
return this;
92+
}
93+
7394
/**
7495
* Set the GCM sender ID for the previous integration
7596
* Only set this if you're migrating from GCM to FCM and they're in different projects / have different sender IDs

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.Before;
1010
import org.junit.Test;
1111
import org.mockito.ArgumentCaptor;
12+
import org.mockito.Mockito;
1213
import org.powermock.api.mockito.PowerMockito;
1314
import org.powermock.core.classloader.annotations.PrepareForTest;
1415
import org.robolectric.RuntimeEnvironment;
@@ -27,6 +28,7 @@
2728
import static junit.framework.Assert.assertTrue;
2829
import static org.mockito.ArgumentMatchers.any;
2930
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.never;
3032
import static org.mockito.Mockito.timeout;
3133
import static org.mockito.Mockito.verify;
3234
import static org.mockito.Mockito.when;
@@ -145,4 +147,73 @@ public void testHandleUniversalLinkRewrite() throws Exception {
145147
assertEquals(url, capturedActionContext.getValue().action.getData());
146148
}
147149

150+
@Test
151+
public void testSetEmailWithAutomaticPushRegistration() throws Exception {
152+
IterableApi.sharedInstance = Mockito.spy(new IterableApi());
153+
IterableApi.initialize(RuntimeEnvironment.application, "fake_key", new IterableConfig.Builder().setPushIntegrationName("pushIntegration").setAutoPushRegistration(true).build());
154+
155+
// Check that setEmail calls registerForPush
156+
IterableApi.getInstance().setEmail("[email protected]");
157+
verify(IterableApi.sharedInstance).registerForPush();
158+
Mockito.reset(IterableApi.sharedInstance);
159+
160+
// Check that setEmail(null) disables the device
161+
IterableApi.getInstance().setEmail(null);
162+
verify(IterableApi.sharedInstance).disablePush();
163+
Mockito.reset(IterableApi.sharedInstance);
164+
}
165+
166+
@Test
167+
public void testSetEmailWithoutAutomaticPushRegistration() throws Exception {
168+
IterableApi.sharedInstance = Mockito.spy(new IterableApi());
169+
IterableApi.initialize(RuntimeEnvironment.application, "fake_key", new IterableConfig.Builder().setPushIntegrationName("pushIntegration").setAutoPushRegistration(false).build());
170+
171+
// Check that setEmail doesn't call registerForPush or disablePush
172+
IterableApi.getInstance().setEmail("[email protected]");
173+
IterableApi.getInstance().setEmail(null);
174+
verify(IterableApi.sharedInstance, never()).registerForPush();
175+
verify(IterableApi.sharedInstance, never()).disablePush();
176+
Mockito.reset(IterableApi.sharedInstance);
177+
}
178+
179+
@Test
180+
public void testSetUserIdWithAutomaticPushRegistration() throws Exception {
181+
IterableApi.sharedInstance = Mockito.spy(new IterableApi());
182+
IterableApi.initialize(RuntimeEnvironment.application, "fake_key", new IterableConfig.Builder().setPushIntegrationName("pushIntegration").setAutoPushRegistration(true).build());
183+
184+
// Check that setUserId calls registerForPush
185+
IterableApi.getInstance().setUserId("userId");
186+
verify(IterableApi.sharedInstance).registerForPush();
187+
Mockito.reset(IterableApi.sharedInstance);
188+
189+
// Check that setUserId(null) disables the device
190+
IterableApi.getInstance().setUserId(null);
191+
verify(IterableApi.sharedInstance).disablePush();
192+
Mockito.reset(IterableApi.sharedInstance);
193+
}
194+
195+
@Test
196+
public void testSetUserIdWithoutAutomaticPushRegistration() throws Exception {
197+
IterableApi.sharedInstance = Mockito.spy(new IterableApi());
198+
IterableApi.initialize(RuntimeEnvironment.application, "fake_key", new IterableConfig.Builder().setPushIntegrationName("pushIntegration").setAutoPushRegistration(false).build());
199+
200+
// Check that setEmail calls registerForPush
201+
IterableApi.getInstance().setUserId("userId");
202+
IterableApi.getInstance().setUserId(null);
203+
verify(IterableApi.sharedInstance, never()).registerForPush();
204+
verify(IterableApi.sharedInstance, never()).disablePush();
205+
Mockito.reset(IterableApi.sharedInstance);
206+
}
207+
208+
@Test
209+
public void testAutomaticPushRegistrationOnInit() throws Exception {
210+
IterableApi.initialize(RuntimeEnvironment.application, "fake_key", new IterableConfig.Builder().setPushIntegrationName("pushIntegration").setAutoPushRegistration(true).build());
211+
IterableApi.getInstance().setEmail("[email protected]");
212+
213+
IterableApi.sharedInstance = Mockito.spy(new IterableApi());
214+
IterableApi.initialize(RuntimeEnvironment.application, "fake_key", new IterableConfig.Builder().setPushIntegrationName("pushIntegration").setAutoPushRegistration(true).build());
215+
verify(IterableApi.sharedInstance).registerForPush();
216+
Mockito.reset(IterableApi.sharedInstance);
217+
}
218+
148219
}

0 commit comments

Comments
 (0)