|
1 | 1 | package com.intercom.reactnative; |
2 | 2 |
|
3 | 3 | import android.app.Activity; |
| 4 | +import android.app.ActivityManager; |
4 | 5 | import android.app.Application; |
5 | 6 | import android.content.Intent; |
6 | 7 | import android.util.Log; |
@@ -82,17 +83,46 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat |
82 | 83 | public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) { |
83 | 84 | try { |
84 | 85 | 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 | + } |
88 | 92 | } |
| 93 | + |
89 | 94 | handleRemotePushWithCustomStack(application, remoteMessage, customStack); |
90 | 95 | } catch (Exception err) { |
91 | 96 | Log.e(NAME, "handleRemotePushMessage error:"); |
92 | 97 | Log.e(NAME, err.toString()); |
93 | 98 | } |
94 | 99 | } |
95 | 100 |
|
| 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 | + |
96 | 126 | public static void sendTokenToIntercom(Application application, @NonNull String token) { |
97 | 127 | intercomPushClient.sendTokenToIntercom(application, token); |
98 | 128 | Log.d(NAME, "sendTokenToIntercom"); |
|
0 commit comments