From 2cc9ff5a41478bc05d3e93624e7f1bc386e489c8 Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 29 Jul 2026 17:07:27 -0500 Subject: [PATCH] Add POST_NOTIFICATION to manifest and start foreground service with type --- android/app/src/main/AndroidManifest.xml | 1 + .../com/audiobookshelf/app/MainActivity.kt | 22 ++++++++++++------- .../app/services/DownloadService.kt | 18 +++++++++++++-- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 1ecfd2bc..3c7cadc6 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" /> + () + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + needed.add(Manifest.permission.READ_EXTERNAL_STORAGE) + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && + ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + needed.add(Manifest.permission.POST_NOTIFICATIONS) + } + if (needed.isNotEmpty()) { + ActivityCompat.requestPermissions(this, needed.toTypedArray(), REQUEST_PERMISSIONS) } } diff --git a/android/app/src/main/java/com/audiobookshelf/app/services/DownloadService.kt b/android/app/src/main/java/com/audiobookshelf/app/services/DownloadService.kt index 453f6412..13a2b60b 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/services/DownloadService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/services/DownloadService.kt @@ -7,6 +7,8 @@ import android.app.PendingIntent import android.app.Service import android.content.Context import android.content.Intent +import android.content.pm.ServiceInfo +import android.os.Build import android.os.IBinder import androidx.core.app.NotificationCompat import com.audiobookshelf.app.R @@ -17,14 +19,17 @@ class DownloadService : Service() { override fun onCreate() { super.onCreate() createChannel() - startForeground(NOTIFICATION_ID, notification(DownloadServiceHost.notificationStrings(this).preparing)) + startForegroundWithType(DownloadServiceHost.notificationStrings(this).preparing) DownloadServiceHost.attachService(this) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { when (intent?.action) { ACTION_CANCEL -> DownloadServiceHost.cancelAll(this) - else -> DownloadServiceHost.ensure(this) + else -> { + startForegroundWithType(DownloadServiceHost.notificationStrings(this).preparing) + DownloadServiceHost.ensure(this) + } } return START_STICKY } @@ -53,6 +58,15 @@ class DownloadService : Service() { } } + private fun startForegroundWithType(text: String) { + val notification = notification(text) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC) + } else { + startForeground(NOTIFICATION_ID, notification) + } + } + private fun notification(text: String, progress: Int = 0, determinate: Boolean = false): Notification { val cancelIntent = PendingIntent.getService( this, 1, Intent(this, DownloadService::class.java).setAction(ACTION_CANCEL), pendingIntentFlags())