Fix active peer notification live updates and background handling

This commit is contained in:
callebtc
2026-01-18 03:24:31 +07:00
parent 85a7a77286
commit 9d1649fc69
@@ -183,21 +183,42 @@ class NotificationManager(
val currentTime = System.currentTimeMillis() val currentTime = System.currentTimeMillis()
val timeSinceLast = currentTime - notificationIntervalManager.lastNetworkNotificationTime val timeSinceLast = currentTime - notificationIntervalManager.lastNetworkNotificationTime
val activePeerNotificationIntervalExceeded = timeSinceLast > ACTIVE_PEERS_NOTIFICATION_TIME_INTERVAL val activePeerNotificationIntervalExceeded = timeSinceLast > ACTIVE_PEERS_NOTIFICATION_TIME_INTERVAL
val newPeers = peers - notificationIntervalManager.recentlySeenPeers
if (isAppInBackground) { val isFreshStart = notificationIntervalManager.recentlySeenPeers.isEmpty()
if (activePeerNotificationIntervalExceeded && newPeers.isNotEmpty()) {
Log.d(TAG, "Showing NEW notification for active peers") // Check if we should alert (Heads-up / Sound)
showNotificationForActivePeers(peers.size, onlyAlertOnce = false) // Only alert if we are in Background AND (it's a fresh start OR enough time passed)
notificationIntervalManager.setLastNetworkNotificationTime(currentTime) val shouldAlert = isAppInBackground && (isFreshStart || activePeerNotificationIntervalExceeded)
notificationIntervalManager.recentlySeenPeers.addAll(newPeers)
} else if (timeSinceLast <= ACTIVE_PEERS_NOTIFICATION_TIME_INTERVAL) { // Check if we should update an existing notification (Silent)
// Update existing notification silently to reflect live count // If alerting, we definitely update.
Log.d(TAG, "Updating active peers count silently") // If not alerting:
showNotificationForActivePeers(peers.size, onlyAlertOnce = true) // - If Background: Update silently (create or update).
notificationIntervalManager.recentlySeenPeers.addAll(newPeers) // - 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) { private fun showNotificationForSender(senderPeerID: String) {