Skip to content

Commit 524a2fd

Browse files
authored
Fix Android deep link push notifications causing app restart in foreground (#368)
* Fix Android deep link push notifications causing app restart in foreground * config update
1 parent f0a2e8b commit 524a2fd

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

android/src/newarch/IntercomModule.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.intercom.reactnative;
22

33
import android.app.Activity;
4+
import android.app.ActivityManager;
45
import android.app.Application;
56
import android.content.Intent;
67
import android.util.Log;
@@ -90,17 +91,46 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat
9091
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
9192
try {
9293
TaskStackBuilder customStack = TaskStackBuilder.create(application);
93-
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
94-
if (launchIntent != null) {
95-
customStack.addNextIntent(launchIntent);
94+
95+
if (!isAppInForeground(application)) {
96+
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
97+
if (launchIntent != null) {
98+
customStack.addNextIntent(launchIntent);
99+
}
96100
}
101+
97102
handleRemotePushWithCustomStack(application, remoteMessage, customStack);
98103
} catch (Exception err) {
99104
Log.e(NAME, "handleRemotePushMessage error:");
100105
Log.e(NAME, err.toString());
101106
}
102107
}
103108

109+
private static boolean isAppInForeground(@NonNull Application application) {
110+
try {
111+
ActivityManager activityManager = (ActivityManager) application.getSystemService(android.content.Context.ACTIVITY_SERVICE);
112+
if (activityManager == null) {
113+
return false;
114+
}
115+
java.util.List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
116+
if (appProcesses == null) {
117+
return false;
118+
}
119+
String packageName = application.getPackageName();
120+
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
121+
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
122+
&& appProcess.processName.equals(packageName)) {
123+
return true;
124+
}
125+
}
126+
return false;
127+
} catch (Exception err) {
128+
Log.e(NAME, "isAppInForeground error:");
129+
Log.e(NAME, err.toString());
130+
return false;
131+
}
132+
}
133+
104134
public static void sendTokenToIntercom(Application application, @NonNull String token) {
105135
if (application == null || token == null || token.isEmpty()) {
106136
Log.w(NAME, "sendTokenToIntercom: application or token is null or empty");

android/src/oldarch/IntercomModule.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.intercom.reactnative;
22

33
import android.app.Activity;
4+
import android.app.ActivityManager;
45
import android.app.Application;
56
import android.content.Intent;
67
import android.util.Log;
@@ -82,17 +83,46 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat
8283
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
8384
try {
8485
TaskStackBuilder customStack = TaskStackBuilder.create(application);
85-
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
86-
if (launchIntent != null) {
87-
customStack.addNextIntent(launchIntent);
86+
87+
if (!isAppInForeground(application)) {
88+
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
89+
if (launchIntent != null) {
90+
customStack.addNextIntent(launchIntent);
91+
}
8892
}
93+
8994
handleRemotePushWithCustomStack(application, remoteMessage, customStack);
9095
} catch (Exception err) {
9196
Log.e(NAME, "handleRemotePushMessage error:");
9297
Log.e(NAME, err.toString());
9398
}
9499
}
95100

101+
private static boolean isAppInForeground(@NonNull Application application) {
102+
try {
103+
ActivityManager activityManager = (ActivityManager) application.getSystemService(android.content.Context.ACTIVITY_SERVICE);
104+
if (activityManager == null) {
105+
return false;
106+
}
107+
java.util.List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
108+
if (appProcesses == null) {
109+
return false;
110+
}
111+
String packageName = application.getPackageName();
112+
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
113+
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
114+
&& appProcess.processName.equals(packageName)) {
115+
return true;
116+
}
117+
}
118+
return false;
119+
} catch (Exception err) {
120+
Log.e(NAME, "isAppInForeground error:");
121+
Log.e(NAME, err.toString());
122+
return false;
123+
}
124+
}
125+
96126
public static void sendTokenToIntercom(Application application, @NonNull String token) {
97127
intercomPushClient.sendTokenToIntercom(application, token);
98128
Log.d(NAME, "sendTokenToIntercom");

0 commit comments

Comments
 (0)