Home > Software design >  Kotlin: Type mismatch: inferred type is String but Context was expected -(notification channel in ko
Kotlin: Type mismatch: inferred type is String but Context was expected -(notification channel in ko

Time:10-06

I am making an app which requires the use of work manager to make a notification, I know how to make notifications in an activity but writing them in a separate kotlin file in the do work method as follows:

class NotificationWorkManager(appContext: Context, workerParams: WorkerParameters):
    Worker(appContext, workerParams) {

    //notification variables
    val CHANNEL_ID = "channel_ID"
    val CHANNEL_NAME = "channel_NAME"
    val NOTIFICATION_ID = 0

    override fun doWork(): Result {


        triggerNotify()
        return Result.success()
    }

    fun triggerNotify(){
        createNotificationChannel()
        //big picture:notification
        val notiStyle = NotificationCompat.BigPictureStyle()
        var remote_picture = BitmapFactory.decodeResource(applicationContext.resources, R.drawable.make_new)
        notiStyle.bigPicture(remote_picture)

//open the app from outside the app
            val intent = Intent(applicationContext, MainActivity::class.java)
            val pendingIntent = TaskStackBuilder.create(applicationContext).run{
                addNextIntentWithParentStack(intent)
                getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
            }
    
//main notification variable
            val notification= NotificationCompat.Builder(applicationContext,CHANNEL_ID)
                .setContentTitle("Workmg")
                .setContentText("basdasdadfhgafgsgsfg")
                .setSmallIcon(R.drawable.ic_bb)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setColor(Color.RED)
                .setStyle(notiStyle)
                .setProgress(100,10,true)//true keeps scroller running
                .setLargeIcon(BitmapFactory.decodeResource(applicationContext.resources, R.drawable.make_new))
                .setContentIntent(pendingIntent)
                .build()
    
            val notificationManager = NotificationManagerCompat.from(applicationContext)
            notificationManager.notify(NOTIFICATION_ID,notification)
        }

    
        fun createNotificationChannel(){
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                val channel= NotificationChannel( CHANNEL_ID,CHANNEL_NAME,
                    NotificationManager.IMPORTANCE_HIGH).apply {
                    lightColor= Color.RED
                    enableLights(true)
                }
                val manager= getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                manager.createNotificationChannel(channel)
            }
        }
    }

My error arises in the last few lines where the manager variable is declared along with a red-wavy line underneath NOTIFICATION_SERVICE ::

Type mismatch: inferred type is String but Context was expected

This does not happen if the code is written directly in the Main Activity which i am not doing in this case becuase iI want to use it in a Work Manager.

I tried using Context.NOTIFICATION_SERVICE but still got the same error.

Please help...

CodePudding user response:

To acquire a system service, we generally need both Context object and some kind of a service identifier (e.g. String). When we acquire a system service from Activity (or e.g. Service) we only need to provide an identifier, as Activity is itself a Context object, so Context is already known. In such a case we use Context.getSystemService() function.

In your case you are outside of the Context, so I'm not sure what is the getSystemService() function you use. Maybe you imported a static function: ContextCompat.getSystemService(). In that case, as you can see in the documentation, you need to provide both Context and service identifier to it.

But anyway, workers have access to the context using applicationContext property, so you should be able to acquire a system service with

val manager= applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
  • Related