21
21
import com .facebook .react .bridge .WritableMap ;
22
22
import com .facebook .react .modules .core .DeviceEventManagerModule ;
23
23
import com .facebook .react .modules .core .RCTNativeAppEventEmitter ;
24
+ import com .iterable .iterableapi .InboxSessionManager ;
24
25
import com .iterable .iterableapi .IterableAction ;
25
26
import com .iterable .iterableapi .IterableActionContext ;
26
27
import com .iterable .iterableapi .IterableApi ;
32
33
import com .iterable .iterableapi .IterableInAppCloseAction ;
33
34
import com .iterable .iterableapi .IterableInAppHandler ;
34
35
import com .iterable .iterableapi .IterableInAppLocation ;
36
+ import com .iterable .iterableapi .IterableInAppManager ;
35
37
import com .iterable .iterableapi .IterableInAppMessage ;
38
+ import com .iterable .iterableapi .IterableInboxSession ;
36
39
import com .iterable .iterableapi .IterableLogger ;
37
40
import com .iterable .iterableapi .IterableUrlHandler ;
38
41
import com .iterable .iterableapi .RNIterableInternal ;
39
42
40
-
41
43
import org .json .JSONArray ;
42
44
import org .json .JSONException ;
43
45
import org .json .JSONObject ;
44
46
47
+ import java .util .List ;
45
48
import java .util .concurrent .CountDownLatch ;
46
49
import java .util .concurrent .TimeUnit ;
47
50
48
- public class RNIterableAPIModule extends ReactContextBaseJavaModule implements IterableUrlHandler , IterableCustomActionHandler , IterableInAppHandler , IterableAuthHandler {
49
-
51
+ public class RNIterableAPIModule extends ReactContextBaseJavaModule implements IterableUrlHandler , IterableCustomActionHandler , IterableInAppHandler , IterableAuthHandler , IterableInAppManager .Listener {
50
52
private final ReactApplicationContext reactContext ;
51
53
private static String TAG = "RNIterableAPIModule" ;
52
54
@@ -58,6 +60,8 @@ public class RNIterableAPIModule extends ReactContextBaseJavaModule implements I
58
60
private CountDownLatch authHandlerCallbackLatch ;
59
61
private String passedAuthToken = null ;
60
62
63
+ private final InboxSessionManager sessionManager = new InboxSessionManager ();
64
+
61
65
public RNIterableAPIModule (ReactApplicationContext reactContext ) {
62
66
super (reactContext );
63
67
this .reactContext = reactContext ;
@@ -94,6 +98,9 @@ public void initializeWithApiKey(String apiKey, ReadableMap configReadableMap, S
94
98
95
99
IterableApi .initialize (reactContext , apiKey , configBuilder .build ());
96
100
IterableApi .getInstance ().setDeviceAttribute ("reactNativeSDKVersion" , version );
101
+
102
+ IterableApi .getInstance ().getInAppManager ().addListener (this );
103
+
97
104
// TODO: Figure out what the error cases are and handle them appropriately
98
105
// This is just here to match the TS types and let the JS thread know when we are done initializing
99
106
promise .resolve (true );
@@ -285,8 +292,8 @@ public void handleAppLink(String uri, Promise promise) {
285
292
// ---------------------------------------------------------------------------------------
286
293
// endregion
287
294
288
- // region Track APIs
289
295
// ---------------------------------------------------------------------------------------
296
+ // region Track APIs
290
297
@ ReactMethod
291
298
public void trackInAppOpen (String messageId , @ Nullable Integer location ) {
292
299
IterableInAppMessage message = RNIterableInternal .getMessageById (messageId );
@@ -345,6 +352,7 @@ public void trackInAppClose(String messageId, Integer location, Integer source,
345
352
346
353
IterableApi .getInstance ().trackInAppClose (inAppMessage , clickedUrl , closeAction , inAppCloseLocation );
347
354
}
355
+
348
356
// ---------------------------------------------------------------------------------------
349
357
// endregion
350
358
@@ -371,6 +379,18 @@ public void getInAppMessages(Promise promise) {
371
379
}
372
380
}
373
381
382
+ @ ReactMethod
383
+ public void getInboxMessages (Promise promise ) {
384
+ IterableLogger .d (TAG , "getInboxMessages" );
385
+ try {
386
+ JSONArray inboxMessageJsonArray = Serialization .serializeInAppMessages (IterableApi .getInstance ().getInAppManager ().getInboxMessages ());
387
+ promise .resolve (Serialization .convertJsonToArray (inboxMessageJsonArray ));
388
+ } catch (JSONException e ) {
389
+ IterableLogger .e (TAG , e .getLocalizedMessage ());
390
+ promise .reject ("" , "Failed to fetch messages with error " + e .getLocalizedMessage ());
391
+ }
392
+ }
393
+
374
394
@ ReactMethod
375
395
public void setInAppShowResponse (Integer number ) {
376
396
IterableLogger .printInfo ();
@@ -414,6 +434,34 @@ public Intent getMainActivityIntent(Context context) {
414
434
// ---------------------------------------------------------------------------------------
415
435
// endregion
416
436
437
+ // ---------------------------------------------------------------------------------------
438
+ // region Inbox In-App Session Tracking APIs
439
+
440
+ @ ReactMethod
441
+ public void startSession (ReadableArray visibleRows ) {
442
+ List <IterableInboxSession .Impression > serializedRows = Serialization .impressionsFromReadableArray (visibleRows );
443
+
444
+ sessionManager .startSession (serializedRows );
445
+ }
446
+
447
+ @ ReactMethod
448
+ public void endSession () {
449
+ sessionManager .endSession ();
450
+ }
451
+
452
+ @ ReactMethod
453
+ public void updateVisibleRows (ReadableArray visibleRows ) {
454
+ List <IterableInboxSession .Impression > serializedRows = Serialization .impressionsFromReadableArray (visibleRows );
455
+
456
+ sessionManager .updateVisibleRows (serializedRows );
457
+ }
458
+
459
+ // ---------------------------------------------------------------------------------------
460
+ // endregion
461
+
462
+ // ---------------------------------------------------------------------------------------
463
+ // region Private Serialization Functions
464
+
417
465
private static Integer [] readableArrayToIntegerArray (ReadableArray array ) {
418
466
if (array == null ) {
419
467
return null ;
@@ -440,6 +488,9 @@ private static JSONObject optSerializedDataFields(ReadableMap dataFields) {
440
488
return dataFieldsJson ;
441
489
}
442
490
491
+ // ---------------------------------------------------------------------------------------
492
+ // endregion
493
+
443
494
// ---------------------------------------------------------------------------------------
444
495
// region IterableSDK callbacks
445
496
@@ -528,6 +579,9 @@ public void removeListeners(Integer count) {
528
579
// ---------------------------------------------------------------------------------------
529
580
// endregion
530
581
582
+ // ---------------------------------------------------------------------------------------
583
+ // region Misc Bridge Functions
584
+
531
585
@ ReactMethod
532
586
public void passAlongAuthToken (String authToken ) {
533
587
passedAuthToken = authToken ;
@@ -541,11 +595,19 @@ public void sendEvent(@NonNull String eventName, @Nullable Object eventData) {
541
595
reactContext .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class ).emit (eventName , eventData );
542
596
}
543
597
598
+ @ Override
599
+ public void onInboxUpdated () {
600
+ sendEvent (EventName .receivedIterableInboxChanged .name (), null );
601
+ }
602
+
603
+ // ---------------------------------------------------------------------------------------
604
+ // endregion
544
605
}
545
606
546
607
enum EventName {
547
608
handleUrlCalled ,
548
609
handleCustomActionCalled ,
549
610
handleInAppCalled ,
550
- handleAuthCalled
611
+ handleAuthCalled ,
612
+ receivedIterableInboxChanged
551
613
}
0 commit comments