Skip to content

Commit 2d665d4

Browse files
committed
Fix Android deep link push notifications causing app restart in foreground
1 parent 09bb07a commit 2d665d4

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

android/src/newarch/IntercomModule.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import androidx.annotation.NonNull;
1010
import androidx.annotation.Nullable;
11+
import androidx.lifecycle.Lifecycle;
12+
import androidx.lifecycle.ProcessLifecycleOwner;
1113

1214
import com.facebook.react.bridge.Promise;
1315
import com.facebook.react.bridge.ReactApplicationContext;
@@ -90,17 +92,34 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat
9092
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
9193
try {
9294
TaskStackBuilder customStack = TaskStackBuilder.create(application);
93-
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
94-
if (launchIntent != null) {
95-
customStack.addNextIntent(launchIntent);
95+
96+
if (!isAppInForeground(application)) {
97+
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
98+
if (launchIntent != null) {
99+
customStack.addNextIntent(launchIntent);
100+
}
96101
}
102+
97103
handleRemotePushWithCustomStack(application, remoteMessage, customStack);
98104
} catch (Exception err) {
99105
Log.e(NAME, "handleRemotePushMessage error:");
100106
Log.e(NAME, err.toString());
101107
}
102108
}
103109

110+
private static boolean isAppInForeground(@NonNull Application application) {
111+
try {
112+
return ProcessLifecycleOwner.get()
113+
.getLifecycle()
114+
.getCurrentState()
115+
.isAtLeast(Lifecycle.State.STARTED);
116+
} catch (Exception err) {
117+
Log.e(NAME, "isAppInForeground error:");
118+
Log.e(NAME, err.toString());
119+
return false;
120+
}
121+
}
122+
104123
public static void sendTokenToIntercom(Application application, @NonNull String token) {
105124
if (application == null || token == null || token.isEmpty()) {
106125
Log.w(NAME, "sendTokenToIntercom: application or token is null or empty");

android/src/oldarch/IntercomModule.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import androidx.annotation.NonNull;
1010
import androidx.annotation.Nullable;
11+
import androidx.lifecycle.Lifecycle;
12+
import androidx.lifecycle.ProcessLifecycleOwner;
1113

1214
import com.facebook.react.bridge.Promise;
1315
import com.facebook.react.bridge.ReactApplicationContext;
@@ -82,17 +84,34 @@ public static void handleRemotePushWithCustomStack(@NonNull Application applicat
8284
public static void handleRemotePushMessage(@NonNull Application application, RemoteMessage remoteMessage) {
8385
try {
8486
TaskStackBuilder customStack = TaskStackBuilder.create(application);
85-
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
86-
if (launchIntent != null) {
87-
customStack.addNextIntent(launchIntent);
87+
88+
if (!isAppInForeground(application)) {
89+
Intent launchIntent = application.getPackageManager().getLaunchIntentForPackage(application.getPackageName());
90+
if (launchIntent != null) {
91+
customStack.addNextIntent(launchIntent);
92+
}
8893
}
94+
8995
handleRemotePushWithCustomStack(application, remoteMessage, customStack);
9096
} catch (Exception err) {
9197
Log.e(NAME, "handleRemotePushMessage error:");
9298
Log.e(NAME, err.toString());
9399
}
94100
}
95101

102+
private static boolean isAppInForeground(@NonNull Application application) {
103+
try {
104+
return ProcessLifecycleOwner.get()
105+
.getLifecycle()
106+
.getCurrentState()
107+
.isAtLeast(Lifecycle.State.STARTED);
108+
} catch (Exception err) {
109+
Log.e(NAME, "isAppInForeground error:");
110+
Log.e(NAME, err.toString());
111+
return false;
112+
}
113+
}
114+
96115
public static void sendTokenToIntercom(Application application, @NonNull String token) {
97116
intercomPushClient.sendTokenToIntercom(application, token);
98117
Log.d(NAME, "sendTokenToIntercom");

0 commit comments

Comments
 (0)