본문 바로가기

Dead Code/DEPRECATED-KOTLIN

[코틀린코드연습장] NOTIFICATION.... 삽질






드디어 UDEMY 왕초보 강좌의 1장의 마지막 강좌

NOTIFICATION...





생각보다 복잡하니, 일단 만들어 놓고, 복붙으로 쓰는게 좋을것 같다. 




button.setOnClickListener { view ->
var not = getNotificationBuilder("channel1", "1st channel")
not.setTicker("Ticker")
not.setSmallIcon(android.R.drawable.ic_menu_search)

var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
not.setLargeIcon(bitmap)
not.setNumber(100)
not.setAutoCancel(true)
not.setContentTitle("Content Title")
not.setContentText("Content Text")

var notification = not.build()

var mng = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mng.notify(10, notification)

}
}

fun getNotificationBuilder(id:String, name:String) : NotificationCompat.Builder {
var not : NotificationCompat.Builder? = null

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
var channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
// channel.enableLights(true)
// channel.lightColor = Color.RED
// channel.enableVibration(true)
manager.createNotificationChannel(channel)

not = NotificationCompat.Builder(this, id)

}else{
not = NotificationCompat.Builder(this)
}
return not
}



OS버전에 따라 두가지 형태의 NOTIFICATION 빌더를 세팅하고, 적용한다는 것이 핵심. 


뭐가 이리 복잡해.. ;