Skip to content

Commit

Permalink
#634 set foreground service behavior on API 31+
Browse files Browse the repository at this point in the history
  • Loading branch information
gotev committed Oct 15, 2023
1 parent bf5c01f commit e4ac7ff
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.IBinder
import android.os.PowerManager
import androidx.core.app.NotificationCompat
Expand Down Expand Up @@ -167,7 +168,7 @@ class UploadService : Service() {
}

private fun stopServiceForeground() {
if (Build.VERSION.SDK_INT >= 24) {
if (SDK_INT >= 24) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
@Suppress("DEPRECATION")
Expand Down Expand Up @@ -213,11 +214,16 @@ class UploadService : Service() {
"Starting UploadService. Debug info: $UploadServiceConfig"
}

val notification = NotificationCompat.Builder(this, UploadServiceConfig.defaultNotificationChannel!!)
val builder = NotificationCompat.Builder(this, UploadServiceConfig.defaultNotificationChannel!!)
.setSmallIcon(android.R.drawable.ic_menu_upload)
.setOngoing(true)
.setGroup(UploadServiceConfig.namespace)
.build()

if (SDK_INT >= 31) {
builder.foregroundServiceBehavior = Notification.FOREGROUND_SERVICE_IMMEDIATE
}

val notification = builder.build()

startForeground(UPLOAD_NOTIFICATION_BASE_ID, notification)

Expand Down

0 comments on commit e4ac7ff

Please sign in to comment.