<repo_name>angelorobsonmelo/WorkerRequestSample<file_sep>/settings.gradle
rootProject.name='WorkkerRequestSample'
include ':app'
<file_sep>/app/src/main/java/br/com/angelorobson/www/workkerrequestsample/MainActivity.kt
package br.com.angelorobson.www.workkerrequestsample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
import androidx.work.workDataOf
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {

    companion object {
        const val ID = "id"
    }

    var time = 0

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

        btn_start_worker.setOnClickListener {
            executeWorker(time++)
        }
    }

    private fun executeWorker(time: Int) {
        val workManager = WorkManager.getInstance(this)
        // workDataOf (part of KTX) converts a list of pairs to a [Data] object.
        val imageData = workDataOf(ID to time)

        val uploadWorkRequest = OneTimeWorkRequestBuilder<UploadWorker>()
            .setInputData(imageData)
            .setInitialDelay(10, TimeUnit.SECONDS)
            .build()

        workManager.enqueue(uploadWorkRequest)
        val status = workManager.getWorkInfoByIdLiveData(uploadWorkRequest.id)
        status.observe(this, Observer {
            if (it.state == WorkInfo.State.SUCCEEDED) {
                Log.d("Times", "time= ${it.outputData.getInt(ID, 0)}")
                Toast.makeText(
                    this@MainActivity,
                    "time= ${it.outputData.getInt(ID, 0)}",
                    Toast.LENGTH_SHORT
                ).show()
            }
        })
    }
}
<file_sep>/app/src/main/java/br/com/angelorobson/www/workkerrequestsample/UploadWorker.kt
package br.com.angelorobson.www.workkerrequestsample

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import androidx.work.Worker
import androidx.work.WorkerParameters
import androidx.work.workDataOf

class UploadWorker(private val appContext: Context, workerParams: WorkerParameters) :
    Worker(appContext, workerParams) {

    override fun doWork(): Result {

        // Get the input
        val idInput = inputData.getInt(MainActivity.ID, 0)

        // Create the output of the work
        val outputData = workDataOf(MainActivity.ID to idInput)

        val notificationManager =
            appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val notification =
            getNotification(
                idInput.toString(),
                appContext,
                notificationManager,
                id.toString()
            )

        notificationManager.notify(idInput, notification)

        // Return the output
        return Result.success(outputData)

    }

    private fun getNotification(
        content: String,
        context: Context?,
        manager: NotificationManager,
        id: String
    ): Notification {
        val builder = NotificationCompat.Builder(context?.applicationContext)
            .setContentText(content)
            .setTicker("Alerta")
            .setAutoCancel(false)
            .setSmallIcon(R.mipmap.ic_launcher)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val chanel =
                NotificationChannel(id, id, NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(chanel)
            builder.setChannelId(id)
        }

        return builder.build()
    }
}