1
1
'use strict'
2
2
3
- import { NativeModules , NativeEventEmitter , Linking } from 'react-native'
3
+ import { NativeModules , NativeEventEmitter , Linking , Platform } from 'react-native'
4
4
import {
5
5
IterableInAppShowResponse ,
6
6
IterableInAppLocation ,
@@ -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 )
@@ -313,6 +321,13 @@ class Iterable {
313
321
RNIterableAPI . updateCart ( items )
314
322
}
315
323
324
+ static wakeApp ( ) {
325
+ if ( Platform . OS === "android" ) {
326
+ console . log ( 'Attempting to wake the app' )
327
+ RNIterableAPI . wakeApp ( ) ;
328
+ }
329
+ }
330
+
316
331
/**
317
332
*
318
333
* @param {number } total
@@ -426,57 +441,75 @@ class Iterable {
426
441
}
427
442
428
443
// 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 ) {
431
452
RNEventEmitter . addListener (
432
453
EventName . handleUrlCalled ,
433
454
( dict ) => {
434
455
const url = dict [ "url" ]
435
456
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 )
442
465
}
443
466
}
444
467
)
445
468
}
446
469
447
- if ( config . customActionHandler ) {
470
+ if ( Iterable . savedConfig . customActionHandler ) {
448
471
RNEventEmitter . addListener (
449
472
EventName . handleCustomActionCalled ,
450
473
( dict ) => {
451
474
const action = IterableAction . fromDict ( dict [ "action" ] )
452
475
const context = IterableActionContext . fromDict ( dict [ "context" ] )
453
- config . customActionHandler ! ( action , context )
476
+ Iterable . savedConfig . customActionHandler ! ( action , context )
454
477
}
455
478
)
456
479
}
457
480
458
- if ( config . inAppHandler ) {
481
+ if ( Iterable . savedConfig . inAppHandler ) {
459
482
RNEventEmitter . addListener (
460
483
EventName . handleInAppCalled ,
461
484
( messageDict ) => {
462
485
const message = IterableInAppMessage . fromDict ( messageDict )
463
- const result = config . inAppHandler ! ( message )
486
+ const result = Iterable . savedConfig . inAppHandler ! ( message )
464
487
RNIterableAPI . setInAppShowResponse ( result )
465
488
}
466
489
)
467
490
}
468
491
469
- if ( config . authHandler ) {
492
+ if ( Iterable . savedConfig . authHandler ) {
470
493
RNEventEmitter . addListener (
471
494
EventName . handleAuthCalled ,
472
495
( ) => {
473
- config . authHandler ! ( )
496
+ Iterable . savedConfig . authHandler ! ( )
474
497
. then ( authToken => {
475
498
RNIterableAPI . passAlongAuthToken ( authToken )
476
499
} )
477
500
}
478
501
)
479
502
}
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
+ }
480
513
}
481
514
482
515
private static getVersionFromPackageJson ( ) : string {
0 commit comments