2121import com .facebook .react .bridge .WritableMap ;
2222import com .facebook .react .modules .core .DeviceEventManagerModule ;
2323import com .facebook .react .modules .core .RCTNativeAppEventEmitter ;
24+ import com .iterable .iterableapi .InboxSessionManager ;
2425import com .iterable .iterableapi .IterableAction ;
2526import com .iterable .iterableapi .IterableActionContext ;
2627import com .iterable .iterableapi .IterableApi ;
3233import com .iterable .iterableapi .IterableInAppCloseAction ;
3334import com .iterable .iterableapi .IterableInAppHandler ;
3435import com .iterable .iterableapi .IterableInAppLocation ;
36+ import com .iterable .iterableapi .IterableInAppManager ;
3537import com .iterable .iterableapi .IterableInAppMessage ;
38+ import com .iterable .iterableapi .IterableInboxSession ;
3639import com .iterable .iterableapi .IterableLogger ;
3740import com .iterable .iterableapi .IterableUrlHandler ;
3841import com .iterable .iterableapi .RNIterableInternal ;
3942
40-
4143import org .json .JSONArray ;
4244import org .json .JSONException ;
4345import org .json .JSONObject ;
4446
47+ import java .util .List ;
4548import java .util .concurrent .CountDownLatch ;
4649import java .util .concurrent .TimeUnit ;
4750
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 {
5052 private final ReactApplicationContext reactContext ;
5153 private static String TAG = "RNIterableAPIModule" ;
5254
@@ -58,6 +60,8 @@ public class RNIterableAPIModule extends ReactContextBaseJavaModule implements I
5860 private CountDownLatch authHandlerCallbackLatch ;
5961 private String passedAuthToken = null ;
6062
63+ private final InboxSessionManager sessionManager = new InboxSessionManager ();
64+
6165 public RNIterableAPIModule (ReactApplicationContext reactContext ) {
6266 super (reactContext );
6367 this .reactContext = reactContext ;
@@ -94,6 +98,9 @@ public void initializeWithApiKey(String apiKey, ReadableMap configReadableMap, S
9498
9599 IterableApi .initialize (reactContext , apiKey , configBuilder .build ());
96100 IterableApi .getInstance ().setDeviceAttribute ("reactNativeSDKVersion" , version );
101+
102+ IterableApi .getInstance ().getInAppManager ().addListener (this );
103+
97104 // TODO: Figure out what the error cases are and handle them appropriately
98105 // This is just here to match the TS types and let the JS thread know when we are done initializing
99106 promise .resolve (true );
@@ -285,8 +292,8 @@ public void handleAppLink(String uri, Promise promise) {
285292 // ---------------------------------------------------------------------------------------
286293 // endregion
287294
288- // region Track APIs
289295 // ---------------------------------------------------------------------------------------
296+ // region Track APIs
290297 @ ReactMethod
291298 public void trackInAppOpen (String messageId , @ Nullable Integer location ) {
292299 IterableInAppMessage message = RNIterableInternal .getMessageById (messageId );
@@ -345,6 +352,7 @@ public void trackInAppClose(String messageId, Integer location, Integer source,
345352
346353 IterableApi .getInstance ().trackInAppClose (inAppMessage , clickedUrl , closeAction , inAppCloseLocation );
347354 }
355+
348356 // ---------------------------------------------------------------------------------------
349357 // endregion
350358
@@ -371,6 +379,18 @@ public void getInAppMessages(Promise promise) {
371379 }
372380 }
373381
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+
374394 @ ReactMethod
375395 public void setInAppShowResponse (Integer number ) {
376396 IterableLogger .printInfo ();
@@ -414,6 +434,34 @@ public Intent getMainActivityIntent(Context context) {
414434 // ---------------------------------------------------------------------------------------
415435 // endregion
416436
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+
417465 private static Integer [] readableArrayToIntegerArray (ReadableArray array ) {
418466 if (array == null ) {
419467 return null ;
@@ -440,6 +488,9 @@ private static JSONObject optSerializedDataFields(ReadableMap dataFields) {
440488 return dataFieldsJson ;
441489 }
442490
491+ // ---------------------------------------------------------------------------------------
492+ // endregion
493+
443494 // ---------------------------------------------------------------------------------------
444495 // region IterableSDK callbacks
445496
@@ -528,6 +579,9 @@ public void removeListeners(Integer count) {
528579 // ---------------------------------------------------------------------------------------
529580 // endregion
530581
582+ // ---------------------------------------------------------------------------------------
583+ // region Misc Bridge Functions
584+
531585 @ ReactMethod
532586 public void passAlongAuthToken (String authToken ) {
533587 passedAuthToken = authToken ;
@@ -541,11 +595,19 @@ public void sendEvent(@NonNull String eventName, @Nullable Object eventData) {
541595 reactContext .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class ).emit (eventName , eventData );
542596 }
543597
598+ @ Override
599+ public void onInboxUpdated () {
600+ sendEvent (EventName .receivedIterableInboxChanged .name (), null );
601+ }
602+
603+ // ---------------------------------------------------------------------------------------
604+ // endregion
544605}
545606
546607enum EventName {
547608 handleUrlCalled ,
548609 handleCustomActionCalled ,
549610 handleInAppCalled ,
550- handleAuthCalled
611+ handleAuthCalled ,
612+ receivedIterableInboxChanged
551613}
0 commit comments