package com.gfg.progressnotificationdemo
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import android.os.SystemClock
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.gfg.progressnotificationdemo.R.drawable
class MainActivity : AppCompatActivity(){
private lateinit var notificationManager: NotificationManagerCompat
val channelId = "Progress Notification" as String
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationManager = NotificationManagerCompat.from(this)
}
public fun start(view: View){
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, 0)
val progressMax = 100
val notification =
NotificationCompat.Builder(this, channelId)
.setSmallIcon(drawable.ic_file_download)
.setContentTitle("GeeksforGeeks")
.setContentText("Downloading")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setProgress(progressMax, 0, true)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
notificationManager.notify(1, notification.build())
Thread(Runnable{
SystemClock.sleep(2000)
var progress = 0
while (progress <= progressMax) {
SystemClock.sleep(
1000
)
progress += 20
}
notification.setContentText("Download complete")
.setProgress(0, 0, false)
.setOngoing(false)
notificationManager.notify(1, notification.build())
}).start()
}
}