|
17 | 17 | import android.util.Log; |
18 | 18 |
|
19 | 19 | import androidx.annotation.Nullable; |
| 20 | +import androidx.lifecycle.Lifecycle; |
| 21 | +import androidx.lifecycle.ProcessLifecycleOwner; |
20 | 22 |
|
21 | 23 | import com.google.gson.Gson; |
22 | 24 |
|
@@ -68,6 +70,14 @@ public CustomPushNotification(Context context, Bundle bundle) { |
68 | 70 | public static void clearMessages(int notId) { |
69 | 71 | notificationMessages.remove(Integer.toString(notId)); |
70 | 72 | } |
| 73 | + |
| 74 | + /** |
| 75 | + * Checks if the app is currently in the foreground. |
| 76 | + * Uses ProcessLifecycleOwner to reliably detect app state. |
| 77 | + */ |
| 78 | + public static boolean isAppInForeground() { |
| 79 | + return ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED); |
| 80 | + } |
71 | 81 |
|
72 | 82 | public void onReceived() { |
73 | 83 | String notId = mBundle.getString("notId"); |
@@ -228,6 +238,15 @@ private void showNotification(Bundle bundle, Ejson ejson, String notId) { |
228 | 238 | if (ENABLE_VERBOSE_LOGS) { |
229 | 239 | Log.d(TAG, "[Before add to notificationMessages] notId=" + notId + ", bundle.message length=" + (bundle.getString("message") != null ? bundle.getString("message").length() : 0) + ", bundle.notificationLoaded=" + bundle.getBoolean("notificationLoaded", false)); |
230 | 240 | } |
| 241 | + |
| 242 | + // Don't show notification if app is in foreground |
| 243 | + if (isAppInForeground()) { |
| 244 | + if (ENABLE_VERBOSE_LOGS) { |
| 245 | + Log.d(TAG, "App is in foreground, skipping native notification"); |
| 246 | + } |
| 247 | + return; |
| 248 | + } |
| 249 | + |
231 | 250 | notificationMessages.get(notId).add(bundle); |
232 | 251 | if (ENABLE_VERBOSE_LOGS) { |
233 | 252 | Log.d(TAG, "[After add] notificationMessages[" + notId + "].size=" + notificationMessages.get(notId).size()); |
|
0 commit comments