More notification improvements

This commit is contained in:
Juha Heinanen
2026-03-23 14:15:51 +02:00
parent e1c1d14688
commit fea6fa0e43

View File

@ -1453,6 +1453,17 @@ class BaresipService: Service() {
nm.createNotificationChannel(mediumChannel) nm.createNotificationChannel(mediumChannel)
} }
private fun buildStatusNotification(): Notification {
if (VERSION.SDK_INT >= 31)
snb.foregroundServiceBehavior = Notification.FOREGROUND_SERVICE_IMMEDIATE
snb.setOngoing(true)
snb.setCategory(Notification.CATEGORY_SERVICE)
val notification = snb.build()
notification.flags = notification.flags or Notification.FLAG_NO_CLEAR or
Notification.FLAG_ONGOING_EVENT
return notification
}
@SuppressLint("UnspecifiedImmutableFlag") @SuppressLint("UnspecifiedImmutableFlag")
private fun showStatusNotification() { private fun showStatusNotification() {
val intent = Intent(applicationContext, MainActivity::class.java) val intent = Intent(applicationContext, MainActivity::class.java)
@ -1467,10 +1478,7 @@ class BaresipService: Service() {
.setOngoing(true) .setOngoing(true)
.setStyle(NotificationCompat.DecoratedCustomViewStyle()) .setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout) .setCustomContentView(notificationLayout)
if (VERSION.SDK_INT >= 31) val notification = buildStatusNotification()
snb.foregroundServiceBehavior = Notification.FOREGROUND_SERVICE_IMMEDIATE
val notification = snb.build()
notification.flags = notification.flags or Notification.FLAG_NO_CLEAR
try { try {
if (VERSION.SDK_INT >= 29) if (VERSION.SDK_INT >= 29)
startForeground( startForeground(
@ -1514,7 +1522,7 @@ class BaresipService: Service() {
snb.setCustomContentView(notificationLayout) snb.setCustomContentView(notificationLayout)
// Don't know why, but without the delay the notification is not always updated // Don't know why, but without the delay the notification is not always updated
Timer().schedule(250) { Timer().schedule(250) {
nm.notify(STATUS_NOTIFICATION_ID, snb.build()) nm.notify(STATUS_NOTIFICATION_ID, buildStatusNotification())
} }
} }