Update notification only when necessary

This commit is contained in:
greenart7c3
2025-06-04 15:28:43 -03:00
parent 9e804a6732
commit e4681f5e39
2 changed files with 15 additions and 6 deletions
@@ -6,6 +6,7 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationChannelGroupCompat
@@ -18,6 +19,7 @@ import com.vitorpamplona.ammolite.relays.RelayStats
import kotlinx.coroutines.launch
object AmberRelayStats {
var oldMessage = ""
var connected = 0
var available = 0
init {
@@ -30,7 +32,7 @@ object AmberRelayStats {
}
private val innerCache = mutableMapOf<String, AmberRelayStat>()
fun createNotification(): Notification {
fun createNotification(): Notification? {
val channelId = "ServiceChannel"
val group = NotificationChannelGroupCompat.Builder("ServiceGroup")
.setName(Amber.instance.getString(R.string.service))
@@ -76,6 +78,8 @@ object AmberRelayStats {
connected + ping + sent + received + failed + error
}
if (message == oldMessage && oldMessage.isNotBlank()) return null
oldMessage = message
val contentIntent = Intent(Amber.instance, MainActivity::class.java)
contentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
@@ -105,7 +109,10 @@ object AmberRelayStats {
fun updateNotification() {
val notificationManager = NotificationManagerCompat.from(Amber.instance)
if (ActivityCompat.checkSelfPermission(Amber.instance, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
notificationManager.notify(1, createNotification())
createNotification()?.let {
Log.d(Amber.TAG, "updateNotification")
notificationManager.notify(1, it)
}
}
}
@@ -52,7 +52,6 @@ class ConnectivityService : Service() {
if (BuildConfig.FLAVOR == "offline") return
scope.launch(Dispatchers.IO) {
AmberRelayStats.updateNotification()
Log.d(
"ServiceManager NetworkCallback",
"onCapabilitiesChanged: ${network.networkHandle} hasMobileData ${Amber.instance.isOnMobileDataState.value} hasWifi ${Amber.instance.isOnWifiDataState.value}",
@@ -87,7 +86,9 @@ class ConnectivityService : Service() {
Log.d(Amber.TAG, "onCreate ConnectivityService isStarted: $isStarted")
if (isStarted) return
isStarted = true
startForeground(1, AmberRelayStats.createNotification())
AmberRelayStats.createNotification()?.let {
startForeground(1, it)
}
Amber.instance.applicationIOScope.launch {
while (Amber.instance.isStartingAppState.value) {
delay(1000)
@@ -118,7 +119,6 @@ class ConnectivityService : Service() {
return
}
AmberRelayStats.updateNotification()
val hasAnyRelayDisconnected = Amber.instance.client.getAll().any { !it.isConnected() }
if (hasAnyRelayDisconnected) {
scope.launch {
@@ -153,7 +153,9 @@ class ConnectivityService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(Amber.TAG, "onStartCommand")
startForeground(1, AmberRelayStats.createNotification())
AmberRelayStats.createNotification()?.let {
startForeground(1, it)
}
return START_STICKY
}
}