상세 컨텐츠

본문 제목

[안드로이드 코틀린] Notification 간단한 알림 띄우기

안드로이드

by aries574 2022. 7. 28. 14:53

본문


이번 시간에는 Notification을 이용해서 상태 표시줄에 알림을 띄어주는 방법을 알아보겠습니다.


목차

1. 실행 화면
2. Notification 클래스 만들기
3. 메인 화면 구현 activity_main.xml
4. 메인 코드 구현 MainActivity.kt


1. 실행 화면

 


2. Notification 클래스 만들기

- 설명 -

1. Notification 알림 클래스입니다.

2. 안드로이드 버전이 오레오 8.0 보다 크면 채널을 생성해야 합니다.

3. createChannel

   채널 생성

4. getManager

   NotificationManager 생성

5. getChannelNotification

  Notification 설정

class NotificationHelper(base: Context?) : ContextWrapper(base) {

    //채널 변수 만들기
    private val channelID: String = "channelID"
    private val channelNm: String = "channelName"

    init {
        //안드로이드 버전이 오레오보다 크면
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

            //채널 생성
            createChannel()
        }
    }

    //채널 생성 함수
    private fun createChannel(){

        //객체 생성
        val channel: NotificationChannel =
            NotificationChannel(channelID, channelNm, NotificationManager.IMPORTANCE_DEFAULT)

        //설정
        channel.enableLights(true) //빛
        channel.enableVibration(true) //진동
        channel.lightColor = Color.RED
        channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE

        //생성
        getManager().createNotificationChannel(channel)
    }

    //NotificationManager 생성
    fun getManager(): NotificationManager {

        return getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    }

    //Notification 설정
    fun getChannelNotification(title: String, message: String): NotificationCompat.Builder{

        return NotificationCompat.Builder(applicationContext, channelID)
            .setContentTitle(title) //제목
            .setContentText(message)//내용
            .setSmallIcon(R.drawable.ic_launcher_background) //아이콘
    }
}

 


3. 메인 화면 구현 activity_main.xml

- 설명 -

1. Notification 제목에 들어가는 입력 EditText

2. Notification 내용에 들어가는 입력 EditText

3. Notification 호출하는 Button

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/title_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="title"/>

    <EditText
        android:id="@+id/message_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title_edit"
        android:hint="message"/>

    <Button
        android:id="@+id/noti_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/message_edit"
        android:text="알림 띄우기"
        android:textSize="25sp"/>
</RelativeLayout>

 

4. 메인 코드 구현 MainActivity.kt

- 설명 -

1. notificationHelper = NotificationHelper(this)

  객체 생성

2.  notificationHelper = NotificationHelper(this)

  초기화

3. notificationHelper.getChannelNotification(title, message)

  타이틀, 내용 넘겨 Notification 설정

4. notificationHelper.getManager().notify(1, nb.build())

  Notification 호출

class MainActivity : AppCompatActivity(){

    //Noti 객체 생성
    private lateinit var notificationHelper: NotificationHelper

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

        //객체 생성
        val titleEdit: EditText = findViewById(R.id.title_edit)
        val messageEdit: EditText = findViewById(R.id.message_edit)
        val notiBtn: Button = findViewById(R.id.noti_btn)

        //Noti 초기화
        notificationHelper = NotificationHelper(this)

        //알림 버튼 이벤트
        notiBtn.setOnClickListener {
            val title: String = titleEdit.text.toString()
            val message: String = messageEdit.text.toString()

            //알림 호출
            showNotification(title, message)
        }
    }//onCreate

    //알림 호출
    private fun showNotification(title: String, message: String){

        val nb: NotificationCompat.Builder =
            notificationHelper.getChannelNotification(title, message)

        notificationHelper.getManager().notify(1, nb.build())
    }

}

2022.07.27 - [안드로이드] - [안드로이드 코틀린] DatePicker 달력 만드는 방법

 

[안드로이드 코틀린] DatePicker 달력 만드는 방법

이번 시간에는 DatePicker를 이용해서 달력 만드는 방법에 대하여 알아보겠습니다. 목차 1. 실행 화면 2. 메인 화면 구현 activity_main.xml 3. 메인 코드 구현 MainActivity.kt 1. 실행 화면 2. 메..

aries574.tistory.com

2022.07.26 - [안드로이드] - [안드로이드 코틀린] CalendarView 달력 만드는 방법

 

[안드로이드 코틀린] CalendarView 달력 만드는 방법

이번 시간에는 CalendarView를 이용해서 달력을 만드는 방법에 대하여 알아보겠습니다. 목차 1. 실행 화면 2. 메인 화면 구현 activity_main.xml 3. 메인 코드 구현 MainActivity.kt 1. 실행 화면 2. 메..

aries574.tistory.com

2022.07.25 - [안드로이드] - [안드로이드 코틀린] 스위치(Switch) On, Off 만드는 방법

 

[안드로이드 코틀린] 스위치(Switch) On, Off 만드는 방법

이번 시간에는 스위치(Switch)를 통해 두 개의 상태 값 ON, OFF 만드는 방법에 대하여 알아보겠습니다. 목차 1. 실행 화면 2. 메인 화면 구현 activity_main.xml 3. 메인 코드 구현 MainActivity.kt 1...

aries574.tistory.com

 

반응형

관련글 더보기

댓글 영역