Update Android icon notification in Statusbar regarding a process like download or export on Android 13

54 Views Asked by At

I have an export progress in my app and I want to update the icon according to my export progress in the Statusbar. I have implemented it like below and it works:

private fun buildExportNotification(): Notification {
    
    val icon = getExportIcon()

    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

    val notificationBuilder = NotificationCompat
        .Builder(applicationContext, channelId)
        .setOngoing(true)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
        .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setSmallIcon(icon)
        .build()

    return notificationBuilder
}

When my export progress is updated then I update my notification like below:

    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            exportProgressStateFlow.collect { progress ->
                exportProgress = progress.toInt()
                notification = buildExportNotification()
                with(NotificationManagerCompat.from(applicationContext)) {
                    notify(NOTIFICATION_FOREGROUND_ID, notification)
                }
            }
        }
    }

But the first icon is visible, after that when my export progress is updated, the icon inside the Statusbar will not update, and all the time during this progress the first icon is visible and it does not change. I need to update the icon during the export progress.

1

There are 1 best solutions below

5
Akshay On

To ensure that the icon in the status bar gets updated properly when your export progress changes, you need to make sure that you are updating the notification's small icon whenever the progress changes. Here's how you can modify your code to achieve that:

private var notificationBuilder: NotificationCompat.Builder? = null

private fun buildExportNotification(): Notification {
    val icon = getExportIcon()

    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

    notificationBuilder = NotificationCompat
        .Builder(applicationContext, channelId)
        .setOngoing(true)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
        .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setSmallIcon(icon)

    return notificationBuilder!!.build()
}

// Update notification when export progress changes
lifecycleScope.launch {
    repeatOnLifecycle(Lifecycle.State.STARTED) {
        exportProgressStateFlow.collect { progress ->
            exportProgress = progress.toInt()
            notificationBuilder?.setSmallIcon(getExportIcon()) // Update small icon
            val updatedNotification = notificationBuilder?.build()
            if (updatedNotification != null) {
                notification = updatedNotification
                with(NotificationManagerCompat.from(applicationContext)) {
                    notify(NOTIFICATION_FOREGROUND_ID, notification)
                }
            }
        }
    }
}

In this modified code, I've added a notificationBuilder property to hold the instance of the notification builder. Then, whenever the export progress changes, I update the small icon of the notification builder using setSmallIcon() method. Finally, I rebuild the notification with the updated small icon and notify the notification manager to display the updated notification.