Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions android/src/newarch/IntercomModule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.intercom.reactnative;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
Expand Down Expand Up @@ -90,17 +92,48 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
try {
TaskStackBuilder customStack = TaskStackBuilder.create(application);
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
if (launchIntent != null) {
customStack.addNextIntent(launchIntent);

if (!isAppInForeground(application)) {
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
if (launchIntent != null) {
customStack.addNextIntent(launchIntent);
}
}

handleRemotePushWithCustomStack(application, remoteMessage, customStack);
} catch (Exception err) {
Log.e(NAME, "handleRemotePushMessage error:");
Log.e(NAME, err.toString());
}
}

private static boolean isAppInForeground(@NonNull Application application) {
try {
ActivityManager activityManager = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
return false;
}

List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}

final String packageName = application.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
} catch (Exception err) {
Log.e(NAME, "isAppInForeground error:");
Log.e(NAME, err.toString());
return false;
}
}

public static void sendTokenToIntercom(Application application, @NonNull String token) {
if (application == null || token == null || token.isEmpty()) {
Log.w(NAME, "sendTokenToIntercom: application or token is null or empty");
Expand Down
39 changes: 36 additions & 3 deletions android/src/oldarch/IntercomModule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.intercom.reactnative;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
Expand Down Expand Up @@ -82,17 +84,48 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
try {
TaskStackBuilder customStack = TaskStackBuilder.create(application);
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
if (launchIntent != null) {
customStack.addNextIntent(launchIntent);

if (!isAppInForeground(application)) {
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
if (launchIntent != null) {
customStack.addNextIntent(launchIntent);
}
}

handleRemotePushWithCustomStack(application, remoteMessage, customStack);
} catch (Exception err) {
Log.e(NAME, "handleRemotePushMessage error:");
Log.e(NAME, err.toString());
}
}

private static boolean isAppInForeground(@NonNull Application application) {
try {
ActivityManager activityManager = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
return false;
}

List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}

final String packageName = application.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
} catch (Exception err) {
Log.e(NAME, "isAppInForeground error:");
Log.e(NAME, err.toString());
return false;
}
}

public static void sendTokenToIntercom(Application application, @NonNull String token) {
intercomPushClient.sendTokenToIntercom(application, token);
Log.d(NAME, "sendTokenToIntercom");
Expand Down