Skip to main content

How to implement notification for ongoing downloads using DownloadManager in Kotlin Android

How to implement notification for ongoing downloads using DownloadManager in Kotlin Android.

Here is a detailed step-by-step tutorial on how to implement notification for ongoing downloads using DownloadManager in Kotlin Android:

Step 1: Set up the project

  • Create a new Android project in Kotlin.
  • Open the build.gradle file and add the following dependency:
    implementation 'androidx.core:core-ktx:1.3.2'

Step 2: Create the layout file

  • Open the activity_main.xml file and add a Button with the id "downloadButton":
    <Button
    android:id="@+id/downloadButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start Download" />

Step 3: Implement the download functionality

  • Open the MainActivity.kt file and import the necessary classes:

    import android.app.DownloadManager
    import android.content.BroadcastReceiver
    import android.content.Context
    import android.content.IntentFilter
    import android.net.Uri
    import android.os.Bundle
    import android.os.Environment
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.activity_main.*
  • Declare a variable for the download ID:

    private var downloadId: Long = 0
  • Initialize the DownloadManager and register a BroadcastReceiver to receive the download complete event:

    private lateinit var downloadManager: DownloadManager
    private lateinit var downloadCompleteReceiver: BroadcastReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

    val filter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
    downloadCompleteReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
    val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
    if (id == downloadId) {
    // Handle download complete event
    }
    }
    }
    registerReceiver(downloadCompleteReceiver, filter)
    }
  • Set an OnClickListener for the downloadButton to start the download:

    downloadButton.setOnClickListener {
    startDownload()
    }
  • Implement the startDownload() function:

    private fun startDownload() {
    val request = DownloadManager.Request(Uri.parse("YOUR_DOWNLOAD_URL"))
    .setTitle("File Download")
    .setDescription("Downloading")
    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
    .setDestinationInExternalFilesDir(
    this,
    Environment.DIRECTORY_DOWNLOADS,
    "filename.extension"
    )

    downloadId = downloadManager.enqueue(request)
    }

Step 4: Display a notification for ongoing downloads

  • Add the necessary permission to the AndroidManifest.xml file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • Create a new class named DownloadNotificationHelper:

    import android.app.Notification
    import android.app.NotificationChannel
    import android.app.NotificationManager
    import android.app.PendingIntent
    import android.content.Context
    import android.content.Intent
    import android.os.Build
    import androidx.core.app.NotificationCompat

    class DownloadNotificationHelper(private val context: Context) {
    private val notificationManager: NotificationManager =
    context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    private val channelId = "download_channel"

    init {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(
    channelId,
    "Download",
    NotificationManager.IMPORTANCE_LOW
    )
    notificationManager.createNotificationChannel(channel)
    }
    }

    fun showNotification() {
    val intent = Intent(context, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

    val notification: Notification = NotificationCompat.Builder(context, channelId)
    .setContentTitle("Downloading")
    .setContentText("Download in progress")
    .setSmallIcon(android.R.drawable.stat_sys_download)
    .setContentIntent(pendingIntent)
    .setOngoing(true)
    .build()

    notificationManager.notify(1, notification)
    }

    fun hideNotification() {
    notificationManager.cancel(1)
    }
    }
  • Modify the MainActivity.kt file to use the DownloadNotificationHelper:

    private lateinit var downloadNotificationHelper: DownloadNotificationHelper

    override fun onCreate(savedInstanceState: Bundle?) {
    // ...

    downloadNotificationHelper = DownloadNotificationHelper(this)
    }

    private fun startDownload() {
    // ...

    downloadNotificationHelper.showNotification()
    }

    override fun onDestroy() {
    super.onDestroy()
    unregisterReceiver(downloadCompleteReceiver)
    downloadNotificationHelper.hideNotification()
    }

That's it! You have now implemented notification for ongoing downloads using DownloadManager in Kotlin Android.