diff --git a/android/src/newarch/IntercomModule.java b/android/src/newarch/IntercomModule.java index c892bd10..097caa9f 100644 --- a/android/src/newarch/IntercomModule.java +++ b/android/src/newarch/IntercomModule.java @@ -8,6 +8,8 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.ProcessLifecycleOwner; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; @@ -90,10 +92,14 @@ 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:"); @@ -101,6 +107,19 @@ public static void handleRemotePushMessage(@NonNull Application application, Rem } } + private static boolean isAppInForeground(@NonNull Application application) { + try { + return ProcessLifecycleOwner.get() + .getLifecycle() + .getCurrentState() + .isAtLeast(Lifecycle.State.STARTED); + } 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"); diff --git a/android/src/oldarch/IntercomModule.java b/android/src/oldarch/IntercomModule.java index 32afd733..9db14b2d 100644 --- a/android/src/oldarch/IntercomModule.java +++ b/android/src/oldarch/IntercomModule.java @@ -8,6 +8,8 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.ProcessLifecycleOwner; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; @@ -82,10 +84,14 @@ 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:"); @@ -93,6 +99,19 @@ public static void handleRemotePushMessage(@NonNull Application application, Rem } } + private static boolean isAppInForeground(@NonNull Application application) { + try { + return ProcessLifecycleOwner.get() + .getLifecycle() + .getCurrentState() + .isAtLeast(Lifecycle.State.STARTED); + } 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");