Skip to content

Commit d7e9183

Browse files
committed
Config now saved statically inside Iterable
Context: Android Back Button destroys MainActivity while minimizing the app. Thus front end code is removed from memory. However, it still keeps old config handlers in memory which can hinder rendering view and navigating once we wake app on tap of push notification. This fix will allow all event handlers to use the latest and available IterableConfig as we will statically store the config when initialzing and replacing the static old one.
1 parent c67f5f2 commit d7e9183

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

android/src/main/java/com/iterable/reactnative/RNIterableAPIModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ public void run() {
372372
@ReactMethod
373373
public void wakeApp() {
374374
Intent launcherIntent = getMainActivityIntent(reactContext);
375-
launcherIntent.setFlags(Intent.FLAG_FROM_BACKGROUND);
376-
if (launcherIntent.resolveActivity(getReactApplicationContext().getPackageManager()) != null) {
375+
launcherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
376+
if (launcherIntent.resolveActivity(reactContext.getPackageManager()) != null) {
377377
reactContext.startActivity(launcherIntent);
378378
}
379379
}

ts/Iterable.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -314,8 +322,10 @@ class Iterable {
314322
}
315323

316324
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+
}
319329
}
320330

321331
/**
@@ -431,53 +441,52 @@ class Iterable {
431441
}
432442

433443
// 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) {
436452
RNEventEmitter.addListener(
437453
EventName.handleUrlCalled,
438454
(dict) => {
439455
const url = dict["url"]
440456
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)
450459
}
451460
)
452461
}
453462

454-
if (config.customActionHandler) {
463+
if (Iterable.savedConfig.customActionHandler) {
455464
RNEventEmitter.addListener(
456465
EventName.handleCustomActionCalled,
457466
(dict) => {
458467
const action = IterableAction.fromDict(dict["action"])
459468
const context = IterableActionContext.fromDict(dict["context"])
460-
config.customActionHandler!(action, context)
469+
Iterable.savedConfig.customActionHandler!(action, context)
461470
}
462471
)
463472
}
464473

465-
if (config.inAppHandler) {
474+
if (Iterable.savedConfig.inAppHandler) {
466475
RNEventEmitter.addListener(
467476
EventName.handleInAppCalled,
468477
(messageDict) => {
469478
const message = IterableInAppMessage.fromDict(messageDict)
470-
const result = config.inAppHandler!(message)
479+
const result = Iterable.savedConfig.inAppHandler!(message)
471480
RNIterableAPI.setInAppShowResponse(result)
472481
}
473482
)
474483
}
475484

476-
if (config.authHandler) {
485+
if (Iterable.savedConfig.authHandler) {
477486
RNEventEmitter.addListener(
478487
EventName.handleAuthCalled,
479488
() => {
480-
config.authHandler!()
489+
Iterable.savedConfig.authHandler!()
481490
.then(authToken => {
482491
RNIterableAPI.passAlongAuthToken(authToken)
483492
})
@@ -486,7 +495,7 @@ class Iterable {
486495
}
487496

488497
function callUrlHandler(url: any, context: IterableActionContext) {
489-
if (config.urlHandler!(url, context) == false) {
498+
if (Iterable.savedConfig.urlHandler!(url, context) == false) {
490499
Linking.canOpenURL(url)
491500
.then(canOpen => {
492501
if (canOpen) { Linking.openURL(url) }

0 commit comments

Comments
 (0)