From 9d1649fc693e729b5465a2b32053c8f8e6a24060 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 18 Jan 2026 03:24:31 +0700 Subject: [PATCH] Fix active peer notification live updates and background handling --- .../bitchat/android/ui/NotificationManager.kt | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/ui/NotificationManager.kt b/app/src/main/java/com/bitchat/android/ui/NotificationManager.kt index cb98c1df..c9e40b90 100644 --- a/app/src/main/java/com/bitchat/android/ui/NotificationManager.kt +++ b/app/src/main/java/com/bitchat/android/ui/NotificationManager.kt @@ -183,21 +183,42 @@ class NotificationManager( val currentTime = System.currentTimeMillis() val timeSinceLast = currentTime - notificationIntervalManager.lastNetworkNotificationTime val activePeerNotificationIntervalExceeded = timeSinceLast > ACTIVE_PEERS_NOTIFICATION_TIME_INTERVAL - val newPeers = peers - notificationIntervalManager.recentlySeenPeers + + val isFreshStart = notificationIntervalManager.recentlySeenPeers.isEmpty() - if (isAppInBackground) { - if (activePeerNotificationIntervalExceeded && newPeers.isNotEmpty()) { - Log.d(TAG, "Showing NEW notification for active peers") - showNotificationForActivePeers(peers.size, onlyAlertOnce = false) - notificationIntervalManager.setLastNetworkNotificationTime(currentTime) - notificationIntervalManager.recentlySeenPeers.addAll(newPeers) - } else if (timeSinceLast <= ACTIVE_PEERS_NOTIFICATION_TIME_INTERVAL) { - // Update existing notification silently to reflect live count - Log.d(TAG, "Updating active peers count silently") - showNotificationForActivePeers(peers.size, onlyAlertOnce = true) - notificationIntervalManager.recentlySeenPeers.addAll(newPeers) - } + // Check if we should alert (Heads-up / Sound) + // Only alert if we are in Background AND (it's a fresh start OR enough time passed) + val shouldAlert = isAppInBackground && (isFreshStart || activePeerNotificationIntervalExceeded) + + // Check if we should update an existing notification (Silent) + // If alerting, we definitely update. + // If not alerting: + // - If Background: Update silently (create or update). + // - If Foreground: Update silently ONLY IF notification is already active (don't create new banner over UI). + var shouldUpdate = shouldAlert || isAppInBackground + + if (!shouldUpdate && !isAppInBackground && Build.VERSION.SDK_INT >= 23) { + // Check if the notification is currently active + shouldUpdate = systemNotificationManager.activeNotifications.any { it.id == ACTIVE_PEERS_NOTIFICATION_ID } } + + if (shouldUpdate) { + // If we are alerting, use onlyAlertOnce = false (Sound/Vib). + // If we are just updating/silently posting, use onlyAlertOnce = true. + val onlyAlertOnce = !shouldAlert + + if (shouldAlert) { + Log.d(TAG, "Showing NEW notification for active peers (Heads-up)") + notificationIntervalManager.setLastNetworkNotificationTime(currentTime) + } else { + Log.d(TAG, "Updating active peers notification silently (inBackground=$isAppInBackground)") + } + + showNotificationForActivePeers(peers.size, onlyAlertOnce = onlyAlertOnce) + } + + // Always update tracking + notificationIntervalManager.recentlySeenPeers.addAll(peers) } private fun showNotificationForSender(senderPeerID: String) {