Skip to content

Commit c147da4

Browse files
authored
Merge pull request #222 from Iterable/MOB-3467-App-link-Bug-fix-attempt
[MOB - 3467] - App link bug fix
2 parents 3feb188 + 4f514de commit c147da4

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.iterable.reactnative;
22

3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.content.pm.PackageManager;
36
import android.net.Uri;
47
import android.os.Bundle;
58

@@ -369,6 +372,26 @@ public void run() {
369372
});
370373
}
371374

375+
@ReactMethod
376+
public void wakeApp() {
377+
Intent launcherIntent = getMainActivityIntent(reactContext);
378+
launcherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
379+
if (launcherIntent.resolveActivity(reactContext.getPackageManager()) != null) {
380+
reactContext.startActivity(launcherIntent);
381+
}
382+
}
383+
384+
public Intent getMainActivityIntent(Context context) {
385+
Context appContext = context.getApplicationContext();
386+
PackageManager packageManager = appContext.getPackageManager();
387+
Intent intent = packageManager.getLaunchIntentForPackage(appContext.getPackageName());
388+
if (intent == null) {
389+
intent = new Intent(Intent.ACTION_MAIN, null);
390+
intent.addCategory(Intent.CATEGORY_LAUNCHER);
391+
intent.setPackage(appContext.getPackageName());
392+
}
393+
return intent;
394+
}
372395
// ---------------------------------------------------------------------------------------
373396
// endregion
374397

ts/Iterable.ts

+50-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
import { NativeModules, NativeEventEmitter, Linking } from 'react-native'
3+
import { NativeModules, NativeEventEmitter, Linking, Platform } from 'react-native'
44
import {
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

Comments
 (0)