Add POST_NOTIFICATION to manifest and start foreground service with type

This commit is contained in:
advplyr
2026-07-29 17:07:27 -05:00
parent f8424c03fd
commit 2cc9ff5a41
3 changed files with 31 additions and 10 deletions
+1
View File
@@ -13,6 +13,7 @@
android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" /> android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@@ -40,9 +40,6 @@ class MainActivity : BridgeActivity() {
val storage = SimpleStorage(this) val storage = SimpleStorage(this)
val REQUEST_PERMISSIONS = 1 val REQUEST_PERMISSIONS = 1
var PERMISSIONS_ALL = arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE
)
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
DbManager.initialize(applicationContext) DbManager.initialize(applicationContext)
@@ -98,11 +95,20 @@ class MainActivity : BridgeActivity() {
} }
} }
val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) requestNeededPermissions()
if (permission != PackageManager.PERMISSION_GRANTED) { }
ActivityCompat.requestPermissions(this,
PERMISSIONS_ALL, private fun requestNeededPermissions() {
REQUEST_PERMISSIONS) val needed = mutableListOf<String>()
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)
} }
} }
@@ -7,6 +7,8 @@ import android.app.PendingIntent
import android.app.Service import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder import android.os.IBinder
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.audiobookshelf.app.R import com.audiobookshelf.app.R
@@ -17,14 +19,17 @@ class DownloadService : Service() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
createChannel() createChannel()
startForeground(NOTIFICATION_ID, notification(DownloadServiceHost.notificationStrings(this).preparing)) startForegroundWithType(DownloadServiceHost.notificationStrings(this).preparing)
DownloadServiceHost.attachService(this) DownloadServiceHost.attachService(this)
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) { when (intent?.action) {
ACTION_CANCEL -> DownloadServiceHost.cancelAll(this) ACTION_CANCEL -> DownloadServiceHost.cancelAll(this)
else -> DownloadServiceHost.ensure(this) else -> {
startForegroundWithType(DownloadServiceHost.notificationStrings(this).preparing)
DownloadServiceHost.ensure(this)
}
} }
return START_STICKY 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 { private fun notification(text: String, progress: Int = 0, determinate: Boolean = false): Notification {
val cancelIntent = PendingIntent.getService( val cancelIntent = PendingIntent.getService(
this, 1, Intent(this, DownloadService::class.java).setAction(ACTION_CANCEL), pendingIntentFlags()) this, 1, Intent(this, DownloadService::class.java).setAction(ACTION_CANCEL), pendingIntentFlags())