@@ -27,6 +27,7 @@ public class IterableApi {
27
27
private Context _applicationContext ;
28
28
private String _apiKey ;
29
29
private String _email ;
30
+ private String _userId ;
30
31
private boolean _debugMode ;
31
32
private Bundle _payloadData ;
32
33
private IterableNotificationData _notificationData ;
@@ -39,9 +40,6 @@ public class IterableApi {
39
40
IterableApi (){
40
41
}
41
42
42
- IterableApi (Context context , String apiKey , String email ){
43
- updateData (context , apiKey , email );
44
- }
45
43
//---------------------------------------------------------------------------------------
46
44
//endregion
47
45
@@ -104,6 +102,60 @@ void setNotificationData(IterableNotificationData data) {
104
102
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
105
103
* Should be called whenever the app is opened.
106
104
* @param currentActivity The current activity
105
+ * @param userId The current userId
106
+ * @return stored instance of IterableApi
107
+ */
108
+ public static IterableApi sharedInstanceWithApiKeyWithUserId (Activity currentActivity , String apiKey ,
109
+ String userId )
110
+ {
111
+ return sharedInstanceWithApiKeyWithUserId (currentActivity , apiKey , userId , false );
112
+ }
113
+
114
+ /**
115
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
116
+ * Should be called whenever the app is opened.
117
+ * Allows the IterableApi to be intialized with debugging enabled
118
+ * @param currentActivity The current activity
119
+ * @param userId
120
+ * The current userId@return stored instance of IterableApi
121
+ */
122
+ public static IterableApi sharedInstanceWithApiKeyWithUserId (Activity currentActivity , String apiKey ,
123
+ String userId , boolean debugMode )
124
+ {
125
+ return sharedInstanceWithApiKeyWithUserId ((Context ) currentActivity , apiKey , userId , debugMode );
126
+ }
127
+
128
+ /**
129
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
130
+ * Should be called whenever the app is opened.
131
+ * @param currentContext The current context
132
+ * @param userId The current userId
133
+ * @return stored instance of IterableApi
134
+ */
135
+ public static IterableApi sharedInstanceWithApiKeyWithUserId (Context currentContext , String apiKey ,
136
+ String userId )
137
+ {
138
+ return sharedInstanceWithApiKey (currentContext , apiKey , null , userId , false );
139
+ }
140
+
141
+ /**
142
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
143
+ * Should be called whenever the app is opened.
144
+ * Allows the IterableApi to be intialized with debugging enabled
145
+ * @param currentContext The current context
146
+ * @return stored instance of IterableApi
147
+ */
148
+ public static IterableApi sharedInstanceWithApiKeyWithUserId (Context currentContext , String apiKey ,
149
+ String userId , boolean debugMode )
150
+ {
151
+ return sharedInstanceWithApiKey (currentContext , apiKey , null , userId , debugMode );
152
+ }
153
+
154
+ /**
155
+ * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
156
+ * Should be called whenever the app is opened.
157
+ * @param currentActivity The current activity
158
+ * @param email The current email
107
159
* @return stored instance of IterableApi
108
160
*/
109
161
public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey ,
@@ -117,6 +169,7 @@ public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, Str
117
169
* Should be called whenever the app is opened.
118
170
* Allows the IterableApi to be intialized with debugging enabled
119
171
* @param currentActivity The current activity
172
+ * @param email The current email
120
173
* @return stored instance of IterableApi
121
174
*/
122
175
public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey ,
@@ -128,26 +181,34 @@ public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, Str
128
181
/**
129
182
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
130
183
* Should be called whenever the app is opened.
131
- * @param currentActivity The current activity
184
+ * @param currentContext The current context
185
+ * @param email The current email
132
186
* @return stored instance of IterableApi
133
187
*/
134
- public static IterableApi sharedInstanceWithApiKey (Context currentActivity , String apiKey ,
188
+ public static IterableApi sharedInstanceWithApiKey (Context currentContext , String apiKey ,
135
189
String email )
136
190
{
137
- return sharedInstanceWithApiKey (currentActivity , apiKey , email , false );
191
+ return sharedInstanceWithApiKey (currentContext , apiKey , email , false );
138
192
}
139
193
140
194
/**
141
195
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
142
196
* Should be called whenever the app is opened.
143
197
* Allows the IterableApi to be intialized with debugging enabled
144
198
* @param currentContext The current context
199
+ * @param email The current email
145
200
* @return stored instance of IterableApi
146
201
*/
147
202
public static IterableApi sharedInstanceWithApiKey (Context currentContext , String apiKey ,
148
203
String email , boolean debugMode )
149
204
{
150
- sharedInstance .updateData (currentContext .getApplicationContext (), apiKey , email );
205
+ return sharedInstanceWithApiKey (currentContext , apiKey , email , null , debugMode );
206
+ }
207
+
208
+ private static IterableApi sharedInstanceWithApiKey (Context currentContext , String apiKey ,
209
+ String email , String userId , boolean debugMode )
210
+ {
211
+ sharedInstance .updateData (currentContext .getApplicationContext (), apiKey , email , userId );
151
212
152
213
if (currentContext instanceof Activity ) {
153
214
Activity currentActivity = (Activity ) currentContext ;
@@ -217,7 +278,7 @@ public void track(String eventName, String campaignId, String templateId) {
217
278
public void track (String eventName , String campaignId , String templateId , JSONObject dataFields ) {
218
279
JSONObject requestJSON = new JSONObject ();
219
280
try {
220
- requestJSON . put ( IterableConstants . KEY_EMAIL , _email );
281
+ addEmailOrUserIdToJson ( requestJSON );
221
282
requestJSON .put (IterableConstants .KEY_EVENTNAME , eventName );
222
283
223
284
requestJSON .put (IterableConstants .KEY_CAMPAIGNID , campaignId );
@@ -231,32 +292,9 @@ public void track(String eventName, String campaignId, String templateId, JSONOb
231
292
sendRequest (IterableConstants .ENDPOINT_TRACK , requestJSON );
232
293
}
233
294
234
- public void trackConversion (int campaignId , int templateId ) {
235
- trackConversion (campaignId , templateId , null );
236
- }
237
-
238
- public void trackConversion (int campaignId , int templateId , JSONObject dataFields ) {
239
-
240
- JSONObject requestJSON = new JSONObject ();
241
-
242
- try {
243
- requestJSON .put (IterableConstants .KEY_EMAIL , _email );
244
- requestJSON .put (IterableConstants .KEY_CAMPAIGNID , campaignId );
245
- requestJSON .put (IterableConstants .KEY_TEMPLATE_ID , templateId );
246
- if (dataFields != null ) {
247
- requestJSON .put (IterableConstants .KEY_DATAFIELDS , dataFields );
248
- }
249
- }
250
- catch (JSONException e ) {
251
- e .printStackTrace ();
252
- }
253
-
254
- sendRequest (IterableConstants .ENDPOINT_TRACKCONVERSION , requestJSON );
255
- }
256
-
257
295
public void sendPush (String email , int campaignId ) {
258
- sendPush (email , campaignId , null , null );
259
- }
296
+ sendPush (email , campaignId , null , null );
297
+ }
260
298
261
299
/**
262
300
* Sends a push campaign to an email address at the given time.
@@ -300,26 +338,31 @@ public void sendPush(String email, int campaignId, Date sendAt, JSONObject dataF
300
338
}
301
339
302
340
public void updateEmail (String newEmail ) {
303
- JSONObject requestJSON = new JSONObject ();
341
+ if (_email != null ) {
342
+ JSONObject requestJSON = new JSONObject ();
304
343
305
- try {
306
- requestJSON .put (IterableConstants .KEY_CURRENT_EMAIL , _email );
307
- requestJSON .put (IterableConstants .KEY_NEW_EMAIL , newEmail );
308
- }
309
- catch (JSONException e ) {
310
- e .printStackTrace ();
311
- }
344
+ try {
345
+ requestJSON .put (IterableConstants .KEY_CURRENT_EMAIL , _email );
346
+ requestJSON .put (IterableConstants .KEY_NEW_EMAIL , newEmail );
347
+ }
348
+ catch (JSONException e ) {
349
+ e .printStackTrace ();
350
+ }
312
351
313
- sendRequest (IterableConstants .ENDPOINT_UPDATEEMAIL , requestJSON );
352
+ sendRequest (IterableConstants .ENDPOINT_UPDATEEMAIL , requestJSON );
314
353
315
- _email = newEmail ;
354
+ _email = newEmail ;
355
+ } else {
356
+ IterableLogger .w (TAG , "updateEmail should not be called with a userId. " +
357
+ "Init SDK with sharedInstanceWithApiKey instead of sharedInstanceWithApiKeyWithUserId" );
358
+ }
316
359
}
317
360
318
361
public void updateUser (JSONObject dataFields ) {
319
362
JSONObject requestJSON = new JSONObject ();
320
363
321
364
try {
322
- requestJSON . put ( IterableConstants . KEY_EMAIL , _email );
365
+ addEmailOrUserIdToJson ( requestJSON );
323
366
requestJSON .put (IterableConstants .KEY_DATAFIELDS , dataFields );
324
367
}
325
368
catch (JSONException e ) {
@@ -373,7 +416,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
373
416
JSONObject requestJSON = new JSONObject ();
374
417
375
418
try {
376
- requestJSON . put ( IterableConstants . KEY_EMAIL , _email );
419
+ addEmailOrUserIdToJson ( requestJSON );
377
420
requestJSON .put (IterableConstants .KEY_CAMPAIGNID , campaignId );
378
421
requestJSON .put (IterableConstants .KEY_TEMPLATE_ID , templateId );
379
422
requestJSON .put (IterableConstants .KEY_MESSAGE_ID , messageId );
@@ -392,8 +435,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
392
435
protected void disablePush (String token ) {
393
436
JSONObject requestJSON = new JSONObject ();
394
437
try {
395
- requestJSON .put (IterableConstants .KEY_TOKEN , token );
396
- requestJSON .put (IterableConstants .KEY_EMAIL , _email );
438
+ requestJSON .put (IterableConstants .KEY_TOKEN , token );addEmailOrUserIdToJson (requestJSON );
397
439
}
398
440
catch (JSONException e ) {
399
441
e .printStackTrace ();
@@ -406,10 +448,11 @@ protected void disablePush(String token) {
406
448
407
449
//region Private Fuctions
408
450
//---------------------------------------------------------------------------------------
409
- private void updateData (Context context , String apiKey , String email ) {
451
+ private void updateData (Context context , String apiKey , String email , String userId ) {
410
452
this ._applicationContext = context ;
411
453
this ._apiKey = apiKey ;
412
454
this ._email = email ;
455
+ this ._userId = userId ;
413
456
}
414
457
415
458
private void tryTrackNotifOpen (Intent calledIntent ) {
@@ -434,7 +477,8 @@ private void registerDeviceToken(String applicationName, String token, JSONObjec
434
477
435
478
JSONObject requestJSON = new JSONObject ();
436
479
try {
437
- requestJSON .put (IterableConstants .KEY_EMAIL , _email );
480
+ addEmailOrUserIdToJson (requestJSON );
481
+
438
482
if (dataFields == null ) {
439
483
dataFields = new JSONObject ();
440
484
}
@@ -463,6 +507,18 @@ private void sendRequest(String resourcePath, JSONObject json) {
463
507
new IterableRequest ().execute (request );
464
508
}
465
509
510
+ private void addEmailOrUserIdToJson (JSONObject requestJSON ) {
511
+ try {
512
+ if (_email != null ) {
513
+ requestJSON .put (IterableConstants .KEY_EMAIL , _email );
514
+ } else {
515
+ requestJSON .put (IterableConstants .KEY_USER_ID , _userId );
516
+ }
517
+ } catch (JSONException e ) {
518
+ e .printStackTrace ();
519
+ }
520
+ }
521
+
466
522
//---------------------------------------------------------------------------------------
467
523
//endregion
468
524
0 commit comments