@@ -199,6 +199,12 @@ class Iterable {
199
199
*/
200
200
static inAppManager = new IterableInAppManager ( )
201
201
202
+
203
+ /**
204
+ * savedConfig instance.
205
+ */
206
+ static savedConfig : IterableConfig
207
+
202
208
/**
203
209
*
204
210
* @param {string } apiKey
@@ -207,7 +213,8 @@ class Iterable {
207
213
static initialize ( apiKey : string , config : IterableConfig = new IterableConfig ( ) ) : Promise < boolean > {
208
214
console . log ( "initialize: " + apiKey ) ;
209
215
210
- this . setupEventHandlers ( config )
216
+ Iterable . savedConfig = config
217
+ this . setupEventHandlers ( )
211
218
const version = this . getVersionFromPackageJson ( )
212
219
213
220
return RNIterableAPI . initializeWithApiKey ( apiKey , config . toDict ( ) , version )
@@ -220,7 +227,8 @@ class Iterable {
220
227
static initialize2 ( apiKey : string , config : IterableConfig = new IterableConfig ( ) , apiEndPoint : string ) : Promise < boolean > {
221
228
console . log ( "initialize2: " + apiKey ) ;
222
229
223
- this . setupEventHandlers ( config )
230
+ Iterable . savedConfig = config
231
+ this . setupEventHandlers ( )
224
232
const version = this . getVersionFromPackageJson ( )
225
233
226
234
return RNIterableAPI . initialize2WithApiKey ( apiKey , config . toDict ( ) , version , apiEndPoint )
@@ -314,8 +322,10 @@ class Iterable {
314
322
}
315
323
316
324
static wakeApp ( ) {
317
- console . log ( 'Attempting to wake the app' )
318
- RNIterableAPI . wakeApp ( ) ;
325
+ if ( Platform . OS === "android" ) {
326
+ console . log ( 'Attempting to wake the app' )
327
+ RNIterableAPI . wakeApp ( ) ;
328
+ }
319
329
}
320
330
321
331
/**
@@ -431,53 +441,52 @@ class Iterable {
431
441
}
432
442
433
443
// PRIVATE
434
- private static setupEventHandlers ( config : IterableConfig ) {
435
- 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 ) {
436
452
RNEventEmitter . addListener (
437
453
EventName . handleUrlCalled ,
438
454
( dict ) => {
439
455
const url = dict [ "url" ]
440
456
const context = IterableActionContext . fromDict ( dict [ "context" ] )
441
- if ( Platform . OS === "android" ) {
442
- Iterable . wakeApp ( )
443
- //Give enough time for Activity to wake up.
444
- setTimeout ( ( ) => {
445
- callUrlHandler ( url , context )
446
- } , 1000 )
447
- } else {
448
- callUrlHandler ( url , context )
449
- }
457
+ Iterable . wakeApp ( )
458
+ callUrlHandler ( url , context )
450
459
}
451
460
)
452
461
}
453
462
454
- if ( config . customActionHandler ) {
463
+ if ( Iterable . savedConfig . customActionHandler ) {
455
464
RNEventEmitter . addListener (
456
465
EventName . handleCustomActionCalled ,
457
466
( dict ) => {
458
467
const action = IterableAction . fromDict ( dict [ "action" ] )
459
468
const context = IterableActionContext . fromDict ( dict [ "context" ] )
460
- config . customActionHandler ! ( action , context )
469
+ Iterable . savedConfig . customActionHandler ! ( action , context )
461
470
}
462
471
)
463
472
}
464
473
465
- if ( config . inAppHandler ) {
474
+ if ( Iterable . savedConfig . inAppHandler ) {
466
475
RNEventEmitter . addListener (
467
476
EventName . handleInAppCalled ,
468
477
( messageDict ) => {
469
478
const message = IterableInAppMessage . fromDict ( messageDict )
470
- const result = config . inAppHandler ! ( message )
479
+ const result = Iterable . savedConfig . inAppHandler ! ( message )
471
480
RNIterableAPI . setInAppShowResponse ( result )
472
481
}
473
482
)
474
483
}
475
484
476
- if ( config . authHandler ) {
485
+ if ( Iterable . savedConfig . authHandler ) {
477
486
RNEventEmitter . addListener (
478
487
EventName . handleAuthCalled ,
479
488
( ) => {
480
- config . authHandler ! ( )
489
+ Iterable . savedConfig . authHandler ! ( )
481
490
. then ( authToken => {
482
491
RNIterableAPI . passAlongAuthToken ( authToken )
483
492
} )
@@ -486,7 +495,7 @@ class Iterable {
486
495
}
487
496
488
497
function callUrlHandler ( url : any , context : IterableActionContext ) {
489
- if ( config . urlHandler ! ( url , context ) == false ) {
498
+ if ( Iterable . savedConfig . urlHandler ! ( url , context ) == false ) {
490
499
Linking . canOpenURL ( url )
491
500
. then ( canOpen => {
492
501
if ( canOpen ) { Linking . openURL ( url ) }
0 commit comments