11'use strict'
22
3- import { NativeModules , NativeEventEmitter , Linking } from 'react-native'
3+ import { NativeModules , NativeEventEmitter , Linking , Platform } from 'react-native'
44import {
55 IterableInAppShowResponse ,
66 IterableInAppLocation ,
@@ -199,6 +199,12 @@ class Iterable {
199199 */
200200 static inAppManager = new IterableInAppManager ( )
201201
202+
203+ /**
204+ * savedConfig instance.
205+ */
206+ static savedConfig : IterableConfig
207+
202208 /**
203209 *
204210 * @param {string } apiKey
@@ -207,7 +213,8 @@ class Iterable {
207213 static initialize ( apiKey : string , config : IterableConfig = new IterableConfig ( ) ) : Promise < boolean > {
208214 console . log ( "initialize: " + apiKey ) ;
209215
210- this . setupEventHandlers ( config )
216+ Iterable . savedConfig = config
217+ this . setupEventHandlers ( )
211218 const version = this . getVersionFromPackageJson ( )
212219
213220 return RNIterableAPI . initializeWithApiKey ( apiKey , config . toDict ( ) , version )
@@ -220,7 +227,8 @@ class Iterable {
220227 static initialize2 ( apiKey : string , config : IterableConfig = new IterableConfig ( ) , apiEndPoint : string ) : Promise < boolean > {
221228 console . log ( "initialize2: " + apiKey ) ;
222229
223- this . setupEventHandlers ( config )
230+ Iterable . savedConfig = config
231+ this . setupEventHandlers ( )
224232 const version = this . getVersionFromPackageJson ( )
225233
226234 return RNIterableAPI . initialize2WithApiKey ( apiKey , config . toDict ( ) , version , apiEndPoint )
@@ -313,6 +321,13 @@ class Iterable {
313321 RNIterableAPI . updateCart ( items )
314322 }
315323
324+ static wakeApp ( ) {
325+ if ( Platform . OS === "android" ) {
326+ console . log ( 'Attempting to wake the app' )
327+ RNIterableAPI . wakeApp ( ) ;
328+ }
329+ }
330+
316331 /**
317332 *
318333 * @param {number } total
@@ -426,57 +441,75 @@ class Iterable {
426441 }
427442
428443 // PRIVATE
429- private static setupEventHandlers ( config : IterableConfig ) {
430- if ( config . urlHandler ) {
444+ private static setupEventHandlers ( ) {
445+ //Remove all listeners to avoid duplicate listeners
446+ RNEventEmitter . removeAllListeners ( EventName . handleUrlCalled )
447+ RNEventEmitter . removeAllListeners ( EventName . handleInAppCalled )
448+ RNEventEmitter . removeAllListeners ( EventName . handleCustomActionCalled )
449+ RNEventEmitter . removeAllListeners ( EventName . handleAuthCalled )
450+
451+ if ( Iterable . savedConfig . urlHandler ) {
431452 RNEventEmitter . addListener (
432453 EventName . handleUrlCalled ,
433454 ( dict ) => {
434455 const url = dict [ "url" ]
435456 const context = IterableActionContext . fromDict ( dict [ "context" ] )
436- if ( config . urlHandler ! ( url , context ) == false ) {
437- Linking . canOpenURL ( url )
438- . then ( canOpen => {
439- if ( canOpen ) { Linking . openURL ( url ) }
440- } )
441- . catch ( reason => { console . log ( "could not open url: " + reason ) } )
457+ Iterable . wakeApp ( )
458+ if ( Platform . OS === "android" ) {
459+ //Give enough time for Activity to wake up.
460+ setTimeout ( ( ) => {
461+ callUrlHandler ( url , context )
462+ } , 1000 )
463+ } else {
464+ callUrlHandler ( url , context )
442465 }
443466 }
444467 )
445468 }
446469
447- if ( config . customActionHandler ) {
470+ if ( Iterable . savedConfig . customActionHandler ) {
448471 RNEventEmitter . addListener (
449472 EventName . handleCustomActionCalled ,
450473 ( dict ) => {
451474 const action = IterableAction . fromDict ( dict [ "action" ] )
452475 const context = IterableActionContext . fromDict ( dict [ "context" ] )
453- config . customActionHandler ! ( action , context )
476+ Iterable . savedConfig . customActionHandler ! ( action , context )
454477 }
455478 )
456479 }
457480
458- if ( config . inAppHandler ) {
481+ if ( Iterable . savedConfig . inAppHandler ) {
459482 RNEventEmitter . addListener (
460483 EventName . handleInAppCalled ,
461484 ( messageDict ) => {
462485 const message = IterableInAppMessage . fromDict ( messageDict )
463- const result = config . inAppHandler ! ( message )
486+ const result = Iterable . savedConfig . inAppHandler ! ( message )
464487 RNIterableAPI . setInAppShowResponse ( result )
465488 }
466489 )
467490 }
468491
469- if ( config . authHandler ) {
492+ if ( Iterable . savedConfig . authHandler ) {
470493 RNEventEmitter . addListener (
471494 EventName . handleAuthCalled ,
472495 ( ) => {
473- config . authHandler ! ( )
496+ Iterable . savedConfig . authHandler ! ( )
474497 . then ( authToken => {
475498 RNIterableAPI . passAlongAuthToken ( authToken )
476499 } )
477500 }
478501 )
479502 }
503+
504+ function callUrlHandler ( url : any , context : IterableActionContext ) {
505+ if ( Iterable . savedConfig . urlHandler ! ( url , context ) == false ) {
506+ Linking . canOpenURL ( url )
507+ . then ( canOpen => {
508+ if ( canOpen ) { Linking . openURL ( url ) }
509+ } )
510+ . catch ( reason => { console . log ( "could not open url: " + reason ) } )
511+ }
512+ }
480513 }
481514
482515 private static getVersionFromPackageJson ( ) : string {
0 commit comments