Android(Kotlin)

Andorid notification - 1

----___<<<<< 2021. 4. 24. 21:24

 

class MainActivity : AppCompatActivity() {
private val channelID = "com.bokchi.myapplication.channel1"
private var notificationManager : NotificationManager? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createNotificationChannel(channelID, "DemoChannel", "this is a demo")
button.setOnClickListener {
displayNotification()
}
}
private fun displayNotification(){
val notificationId = 45
val notification = NotificationCompat.Builder(this@MainActivity, channelID)
.setContentTitle("Demo TItle")
.setContentText("This is a demo notifiation")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build()
notificationManager?.notify(notificationId, notification)
}
private fun createNotificationChannel(id : String, name:String, channelDescription:String){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(id,name,importance).apply {
description = channelDescription
}
notificationManager?.createNotificationChannel(channel)
}
}
}
view raw MainActivity.kt hosted with ❤ by GitHub

'Android(Kotlin)' 카테고리의 다른 글

Andorid notification - 3  (0) 2021.04.24
Andorid notification - 2  (0) 2021.04.24
kotlin apply  (0) 2021.04.24
Android intent Serializable  (0) 2021.04.23
Retrofit, Coroutine - 7  (0) 2021.04.23