Skip to content

Commit

Permalink
Merge pull request #222 from Iterable/MOB-3467-App-link-Bug-fix-attempt
Browse files Browse the repository at this point in the history
[MOB - 3467] - App link bug fix
  • Loading branch information
Ayyanchira authored Oct 27, 2021
2 parents 3feb188 + 4f514de commit c147da4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.iterable.reactnative;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

Expand Down Expand Up @@ -369,6 +372,26 @@ public void run() {
});
}

@ReactMethod
public void wakeApp() {
Intent launcherIntent = getMainActivityIntent(reactContext);
launcherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (launcherIntent.resolveActivity(reactContext.getPackageManager()) != null) {
reactContext.startActivity(launcherIntent);
}
}

public Intent getMainActivityIntent(Context context) {
Context appContext = context.getApplicationContext();
PackageManager packageManager = appContext.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(appContext.getPackageName());
if (intent == null) {
intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage(appContext.getPackageName());
}
return intent;
}
// ---------------------------------------------------------------------------------------
// endregion

Expand Down
67 changes: 50 additions & 17 deletions ts/Iterable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import { NativeModules, NativeEventEmitter, Linking } from 'react-native'
import { NativeModules, NativeEventEmitter, Linking, Platform } from 'react-native'
import {
IterableInAppShowResponse,
IterableInAppLocation,
Expand Down Expand Up @@ -199,6 +199,12 @@ class Iterable {
*/
static inAppManager = new IterableInAppManager()


/**
* savedConfig instance.
*/
static savedConfig: IterableConfig

/**
*
* @param {string} apiKey
Expand All @@ -207,7 +213,8 @@ class Iterable {
static initialize(apiKey: string, config: IterableConfig = new IterableConfig()): Promise<boolean> {
console.log("initialize: " + apiKey);

this.setupEventHandlers(config)
Iterable.savedConfig = config
this.setupEventHandlers()
const version = this.getVersionFromPackageJson()

return RNIterableAPI.initializeWithApiKey(apiKey, config.toDict(), version)
Expand All @@ -220,7 +227,8 @@ class Iterable {
static initialize2(apiKey: string, config: IterableConfig = new IterableConfig(), apiEndPoint: string): Promise<boolean> {
console.log("initialize2: " + apiKey);

this.setupEventHandlers(config)
Iterable.savedConfig = config
this.setupEventHandlers()
const version = this.getVersionFromPackageJson()

return RNIterableAPI.initialize2WithApiKey(apiKey, config.toDict(), version, apiEndPoint)
Expand Down Expand Up @@ -313,6 +321,13 @@ class Iterable {
RNIterableAPI.updateCart(items)
}

static wakeApp() {
if (Platform.OS === "android") {
console.log('Attempting to wake the app')
RNIterableAPI.wakeApp();
}
}

/**
*
* @param {number} total
Expand Down Expand Up @@ -426,57 +441,75 @@ class Iterable {
}

// PRIVATE
private static setupEventHandlers(config: IterableConfig) {
if (config.urlHandler) {
private static setupEventHandlers() {
//Remove all listeners to avoid duplicate listeners
RNEventEmitter.removeAllListeners(EventName.handleUrlCalled)
RNEventEmitter.removeAllListeners(EventName.handleInAppCalled)
RNEventEmitter.removeAllListeners(EventName.handleCustomActionCalled)
RNEventEmitter.removeAllListeners(EventName.handleAuthCalled)

if (Iterable.savedConfig.urlHandler) {
RNEventEmitter.addListener(
EventName.handleUrlCalled,
(dict) => {
const url = dict["url"]
const context = IterableActionContext.fromDict(dict["context"])
if (config.urlHandler!(url, context) == false) {
Linking.canOpenURL(url)
.then(canOpen => {
if (canOpen) { Linking.openURL(url) }
})
.catch(reason => { console.log("could not open url: " + reason) })
Iterable.wakeApp()
if (Platform.OS === "android") {
//Give enough time for Activity to wake up.
setTimeout(() => {
callUrlHandler(url, context)
}, 1000)
} else {
callUrlHandler(url, context)
}
}
)
}

if (config.customActionHandler) {
if (Iterable.savedConfig.customActionHandler) {
RNEventEmitter.addListener(
EventName.handleCustomActionCalled,
(dict) => {
const action = IterableAction.fromDict(dict["action"])
const context = IterableActionContext.fromDict(dict["context"])
config.customActionHandler!(action, context)
Iterable.savedConfig.customActionHandler!(action, context)
}
)
}

if (config.inAppHandler) {
if (Iterable.savedConfig.inAppHandler) {
RNEventEmitter.addListener(
EventName.handleInAppCalled,
(messageDict) => {
const message = IterableInAppMessage.fromDict(messageDict)
const result = config.inAppHandler!(message)
const result = Iterable.savedConfig.inAppHandler!(message)
RNIterableAPI.setInAppShowResponse(result)
}
)
}

if (config.authHandler) {
if (Iterable.savedConfig.authHandler) {
RNEventEmitter.addListener(
EventName.handleAuthCalled,
() => {
config.authHandler!()
Iterable.savedConfig.authHandler!()
.then(authToken => {
RNIterableAPI.passAlongAuthToken(authToken)
})
}
)
}

function callUrlHandler(url: any, context: IterableActionContext) {
if (Iterable.savedConfig.urlHandler!(url, context) == false) {
Linking.canOpenURL(url)
.then(canOpen => {
if (canOpen) { Linking.openURL(url) }
})
.catch(reason => { console.log("could not open url: " + reason) })
}
}
}

private static getVersionFromPackageJson(): string {
Expand Down

0 comments on commit c147da4

Please sign in to comment.