Android(Kotlin)

Android Notification

----___<<<<< 2021. 1. 21. 00:03

 

 Android Notification 을 만드는 간단한 방법입니다.

 

 문서는 여기에 있고

 

알림 만들기  |  Android 개발자  |  Android Developers

알림은 사용 중이 아닌 앱의 이벤트에 관한 짧고 시기적절한 정보를 제공합니다. 이 페이지에서는 Android 4.0(API 레벨 14) 이상의 다양한 기능을 사용하여 알림을 만드는 방법을 설명합니다. Android

developer.android.com

 간단하게 코드로 구현해보면 아래와 같습니다.

 

class MainActivity : AppCompatActivity() {
private val CHANNEL_ID = "channel_id_example_01"
private val notificationId = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createNotificationChannel()
val btn = findViewById<Button>(R.id.btn_button)
btn.setOnClickListener {
sendNotification()
}
}
private fun createNotificationChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Notification Title"
val descriptionText = "Notification Description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name,importance).apply {
description = descriptionText
}
val notificationManager : NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
private fun sendNotification(){
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("title")
.setContentText("desc")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
with(NotificationManagerCompat.from(this)) {
notify(notificationId, builder.build())
}
}
}
view raw MainActivity.kt hosted with ❤ by GitHub

 

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

Android Jetpack Navigation  (0) 2021.01.21
Android FCM(Push) Kotlin  (0) 2021.01.21
Android kotlin timeStamp  (0) 2021.01.18
Android backbutton double click finish  (0) 2021.01.17
android inapp review  (0) 2021.01.15