Parameters in a function call? [KOTLIN]

Solution for Parameters in a function call? [KOTLIN]
is Given Below:

I’ve spent some time now trying to get a notification with the battery temperature in it.

At the moment I have managed to make the notification appear. Only when I want to run the code below I get an error with the showNotification() part.

This error indicates:

No value passed for parameter ‘context’ No value passed for parameter
‘intent’

But how do I know what parameters to add to showNotification()?

Anyone who can help me with this?

class MyWorker(context: Context, workerParameters: WorkerParameters) :
    Worker(context, workerParameters) {

    companion object {
        const val CHANNEL_ID = "channel_id"
        const val NOTIFICATION_ID = 1
    }

    override fun doWork(): Result {
        Log.d("success",
            "doWork: Success function called")
        showNotification(HERE DO I GET THE PARAMETER MESSAGE)
        return Result.success()
    }

    private fun showNotification(context: Context, intent: Intent) {

        if (intent?.action == "android.intent.action.BATTERY_CHANGED") {
            val temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10.0
            Toast.makeText(context, "${temp}" + "°C", Toast.LENGTH_SHORT).show()
        }

        val intent = Intent(applicationContext, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val pendingIntent = PendingIntent.getActivity(
            applicationContext,
            0, intent, 0
        )

        val notification = NotificationCompat.Builder(
            applicationContext,
            CHANNEL_ID
        )
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("New Task")
            .setContentText("Subscribe on the channel")
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            val channelName = "Channel Name"
            val channelDescription = "Channel Description"
            val channelImportance = NotificationManager.IMPORTANCE_HIGH

            val channel = NotificationChannel(CHANNEL_ID, channelName, channelImportance).apply {
                description = channelDescription
            }

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

            notificationManager.createNotificationChannel(channel)
        }


        with(NotificationManagerCompat.from(applicationContext)) {
            notify(NOTIFICATION_ID, notification.build())
        }

    }
}

There is the MainActivity

class MainActivity : AppCompatActivity() {

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

    private fun myWorkManager() {
        val constraints = Constraints.Builder()
            .setRequiresCharging(false)
            .setRequiredNetworkType(NetworkType.NOT_REQUIRED)
            .setRequiresCharging(false)
            .setRequiresBatteryNotLow(true)
            .build()

        val myRequest = PeriodicWorkRequest.Builder(
            MyWorker::class.java,
            15,
            TimeUnit.MINUTES
        ).setConstraints(constraints)
            .build()

        WorkManager.getInstance(this)
            .enqueueUniquePeriodicWork(
                "my_id",
                ExistingPeriodicWorkPolicy.KEEP,
                myRequest
            )
    }

    private fun simpleWork() {

        val mRequest: WorkRequest = OneTimeWorkRequestBuilder<MyWorker>()
            .build()

        WorkManager.getInstance(this)
            .enqueue(mRequest)

    }
}

You can pass arguments when you create the Request for your WorkManager like this and pass the arguments.

val builder = Data.Builder()

val intent = Intent() // for example, you get your intent here

val data = workDataOf("mydata" to intent)

builder.putAll(data)
val request = OneTimeWorkRequestBuilder<YourWorker>()
    .setInputData(builder.build())
    .build()

and then receive arguments via

override fun doWork(): Result {

    var intent: Intent? = null 
    intent = inputData.keyValueMap["mydata"] as Intent

    if(intent != null) {
       showNotification(context, intent)
    }

    return Result.success()
}

Do tell if that worked for you, you already have the context inside your Worker.