Skip to content

Commit

Permalink
fix foreground notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
hermannakos committed Aug 16, 2023
1 parent 61167ca commit 155fab5
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,35 @@ class FileDownloadWorker @AssistedInject constructor(

private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

override suspend fun doWork(): Result {
val fileName = inputData.getString(INPUT_FILE_NAME) ?: ""
val fileUrl = inputData.getString(INPUT_FILE_URL) ?: ""
val notificationId = Random.nextInt()
private val fileName = inputData.getString(INPUT_FILE_NAME) ?: ""
private val fileUrl = inputData.getString(INPUT_FILE_URL) ?: ""
private val notificationId = Random.nextInt()

private var foregroundInfo: ForegroundInfo = createForegroundInfo(notificationId, fileName, 0)

override suspend fun doWork(): Result {
registerNotificationChannel(context)

val downloadFileName = createDownloadFileName(fileName)

val downloadedFile =
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), downloadFileName)

setForeground(createForegroundInfo(notificationId, fileName, 0))
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
setForeground(foregroundInfo)
}
var result = Result.retry()

fileDownloadApi.downloadFile(fileUrl).saveFile(downloadedFile)
.collect { downloadState ->
when (downloadState) {
is DownloadState.InProgress -> {
setForeground(createForegroundInfo(notificationId, fileName, downloadState.progress))
foregroundInfo = createForegroundInfo(notificationId, fileName, downloadState.progress)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
setForeground(foregroundInfo)
} else {
updateForegroundNotification()
}
}

is DownloadState.Failure -> {
Expand Down Expand Up @@ -138,8 +147,6 @@ class FileDownloadWorker @AssistedInject constructor(

val notification = NotificationCompat.Builder(applicationContext, FileUploadWorker.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_canvas_logo)
.setProgress(100, 100, false)
.setOngoing(false)
.setContentTitle(context.getString(R.string.downloadSuccessful))
.setContentText(fileName)
.setContentIntent(pendingIntent)
Expand All @@ -150,14 +157,20 @@ class FileDownloadWorker @AssistedInject constructor(
private fun updateNotificationFailed(notificationId: Int, fileName: String) {
val notification = NotificationCompat.Builder(applicationContext, FileUploadWorker.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_canvas_logo)
.setProgress(100, 100, false)
.setOngoing(false)
.setContentTitle(context.getString(R.string.downloadFailed))
.setContentText(fileName)
.build()
notificationManager.notify(notificationId + 1, notification)
}

override suspend fun getForegroundInfo(): ForegroundInfo {
return foregroundInfo
}

private fun updateForegroundNotification() {
notificationManager.notify(notificationId, foregroundInfo.notification)
}

companion object {
const val INPUT_FILE_NAME = "fileName"
const val INPUT_FILE_URL = "fileUrl"
Expand Down

0 comments on commit 155fab5

Please sign in to comment.