Search code examples
androidandroid-notificationsandroid-jetpackandroid-workmanager

Unable to update progress in ProgressBar of Notification using WorkManager


I want to create a long-running worker for downloading a video. As per my understanding, we should call createForegroundInfo() to update the ongoing notification. However, I can't find the information on how should I update the determinate progress bar shown in the notification. As shown below, I tried calling the createForegroundInfo() function whenever the progress is updated but the progress bar in the notification is not getting updated.

Note: As you can see in the below code, I am using setProgress(100, downloadProgress, false) as omitting the line didn't show the progress bar in the notification at all.

private fun createForegroundInfo(downloadProgress: Int, videoTitle: String): ForegroundInfo {
        createNotificationChannel()

        val cancelText = applicationContext.getString(R.string.all_text_cancel)
        val cancelPendingIntent =
            WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)

        val notification = NotificationCompat.Builder(
            applicationContext,
            applicationContext.getString(R.string.app_download_channel_id)
        )
            .setContentTitle(videoTitle)
            .setTicker(videoTitle)
            .setProgress(100, downloadProgress, false)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setOngoing(true)
            .addAction(R.drawable.ic_close, cancelText, cancelPendingIntent)
            .build()

        return ForegroundInfo(notificationId, notification)
    }

I also tried updating the progress as shown below but that only updates the progress observed in the Fragment using LiveData and the progress bar in the notification doesn't get updated.

val lastUpdate = workDataOf("Progress" to progress)
setProgress(lastUpdate)

Also as shown below, I observed that the documentation mentions that we should call setForegroundInfo() and not createForegroundInfo() in order to update the ongoing notification. However, there is no method named setForegroundInfo().

enter image description here

As mentioned, I can't find the method to update the progress bar of the ongoing notification. Any help would be appreciated.


Solution

  • It appears there is a mistake in the documentation and the methods name is setForeground() instead of setForegroundInfo(). I was able to update the progress in the notification by using the code shown below.

    setForeground(createForegroundInfo(progress, videoTitle))
    

    createForegroundInfo()

    private fun createForegroundInfo(downloadProgress: Int, videoTitle: String): ForegroundInfo {
            createDownloadNotificationChannel()
    
            val cancelText = applicationContext.getString(R.string.all_text_cancel)
            val cancelPendingIntent =
                WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)
    
            val notification = NotificationCompat.Builder(
                applicationContext,
                applicationContext.getString(R.string.app_download_channel_id)
            )
                .setContentTitle(videoTitle)
                .setContentText("Downloading")
                .setTicker(videoTitle)
                .setProgress(100, downloadProgress, false)
                .setSmallIcon(R.drawable.ic_stat_notification)
                .setOngoing(true)
                .addAction(R.drawable.ic_close, cancelText, cancelPendingIntent)
                .build()
    
            return ForegroundInfo(notificationId, notification)
        }