gotev / android-upload-service

Easily upload files (Multipart/Binary/FTP out of the box) in the background with progress notification. Support for persistent upload requests, customizations and custom plugins.
Apache License 2.0
2.83k stars 690 forks source link

Single notification about all ongoing uploads #640

Closed swizes closed 11 months ago

swizes commented 1 year ago

Is your feature request related to a problem? Please describe.

Creating single notification for all active upload tasks

Describe the solution you'd like

Hello, first of all thanks for the amazing library. From what I have understood, we can't disable the notification per Android requirements, howewer I would like to know, if it is somehow possible to create a single notification with average progress of all upload tasks?

Are you willing to implement it and maintain it?

gotev commented 1 year ago

Hi @swizes you can start from here: https://github.com/gotev/android-upload-service/wiki/Configuration#notification-handler

swizes commented 1 year ago

I was able to set a working example out of it. Thanks for the hint

This is from my SingleNotificationHandler class

override fun updateNotification(
    notificationManager: NotificationManager,
    notificationBuilder: NotificationCompat.Builder,
    tasks: Map<String, TaskData>
  ): NotificationCompat.Builder? {
    // Calculate total progress for all tasks
    var totalUploadedBytes = 0L
    var totalTotalBytes = 0L

    tasks.values.forEach { taskData ->
      totalUploadedBytes += taskData.info.uploadedBytes
      totalTotalBytes += taskData.info.totalBytes
    }

    // Calculate progress percentage
    val totalProgress = if (totalTotalBytes > 0) {
      (totalUploadedBytes * 100 / totalTotalBytes).toInt()
    } else {
      0
    }

    // Return null to not update the notification
    return notificationBuilder
      .setContentTitle("${tasks.size} Uploads")
      .setContentText("${tasks.values.count { it.status == TaskStatus.InProgress }} in progress")
      .setProgress(100, totalProgress, false) // Set the progress bar
      .setSmallIcon(android.R.drawable.ic_menu_upload)
  }

My android knowledge is like 6-7 years old and quite limited, I am using your library for a React Native project so please excuse my (most likely) beginner questions :)

The above method works perfectly and updates the progress bar as I have expected. I would like to know what is causing the notification to disappear when all uploads are completed. I would like have more control when to dismiss the notification or create another regular notification about "Uploads Completed"

Also in the doc you mentioned to call removeTask(uploadId)

So If I understood it correctly, you advise to do this in my NotificationHandler class

  override fun onCompleted(
    info: UploadInfo,
    notificationId: Int,
    notificationConfig: UploadNotificationConfig
  ) {
    super.onCompleted(info, notificationId, notificationConfig)
    removeTask(info.uploadId)
  }

  override fun onError(
    info: UploadInfo,
    notificationId: Int,
    notificationConfig: UploadNotificationConfig,
    exception: Throwable
  ) {
    super.onError(info, notificationId, notificationConfig, exception)
    removeTask(info.uploadId)
  }

Now I wonder if I can do this once all uploads are completed, so I show to the user correct overall progress and upload number, while there is at least 1 upload in progress

gotev commented 1 year ago

I wonder if I can do this once all uploads are completed, so I show to the user correct overall progress and upload number, while there is at least 1 upload in progress

You can either keep a list of completed tasks (and then call removeTask for each one when the last one completes) or a separate list.