Skip to content

Instantly share code, notes, and snippets.

@iambaljeet
Created May 7, 2021 18:42
Show Gist options
  • Save iambaljeet/57ba662fa3c130711c5692274067926d to your computer and use it in GitHub Desktop.
Save iambaljeet/57ba662fa3c130711c5692274067926d to your computer and use it in GitHub Desktop.
class ForegroundWorker(context: Context, parameters: WorkerParameters) :
CoroutineWorker(context, parameters) {
private val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
override suspend fun doWork(): ListenableWorker.Result = withContext(Dispatchers.IO) {
setForeground(createForegroundInfo())
return@withContext runCatching {
runTask()
Result.success()
}.getOrElse {
Result.failure()
}
}
//Fake long running task for 60 seconds
private suspend fun runTask() {
delay(60000)
}
//Creates notifications for service
private fun createForegroundInfo(): ForegroundInfo {
val id = "1225"
val channelName = "Downloads Notification"
val title = "Downloading"
val cancel = "Cancel"
val body = "Long running task is running"
val intent = WorkManager.getInstance(applicationContext)
.createCancelPendingIntent(getId())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel(id, channelName)
}
val notification = NotificationCompat.Builder(applicationContext, id)
.setContentTitle(title)
.setTicker(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_notification)
.setOngoing(true)
.addAction(android.R.drawable.ic_delete, cancel, intent)
.build()
return ForegroundInfo(1, notification)
}
@RequiresApi(Build.VERSION_CODES.O)
private fun createChannel(id: String, channelName: String) {
notificationManager.createNotificationChannel(
NotificationChannel(id, channelName, NotificationManager.IMPORTANCE_DEFAULT)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment