Deperecated/Android_강의

안드로이드 - Notification

누알라리 2020. 2. 14. 00:24
1. Notification이란?

- Notification은 애플리케이션과 별도로 관리되는 메세지이다.

- Notification 메세지를 OS에게 요청하면 OS는 알림 창 영역에 알림 메세지를 표시한다.

- 화면을 가지지 않는 실행단위에서 메세지를 표시할 때 주로 사용한다.

 

2. 특징

- 사용자가 메세지를 확인하거나 제거하기 전까지 메세지가 유지된다.

- 메세지를 터치하면 지정된 Activity가 실행되어 애플리케이션 실행을 유도할 수 있다.

- 안드로이드 4.0 이하, 4.0~7.1버전, 그 이상 버전으로 3가지 Notification이 있다.

 

- 안드로이드 8.0부터는 노티피케이션 채널이라는게 생겼다.

 

3. Notification Channel

- 안드로이드 8.0부터 새롭게 추가된 기능으로 사용자가 애플리케이션의 알림 메세지를 출력하지 않도록 설정하면 모든 메세지가 출력되지 않는다.

- Notification Channel을 이용하면 알림 메세지를 채널이라는 그룹으로 묶을 수 있으며 같은 채널 별로 메세지에 대한 설정을 따로 할 수 있게 된다.

 

4. Notification Builder 생성

- 안드로이드 버전별로 나눠준다.

    fun getNotificationBuilder1(id:String, name:String) : NotificationCompat.Builder{
        var builder: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
            manager.createNotificationChannel(channel)

            //채널의 아이디를 넣어서 채널 아이디가 세팅된게 넘어가면서 채널별로 그룹화해서 노피티케이션을 만들 수 있다.
            builder = NotificationCompat.Builder(this, id)

        }
        else
        {
            // 8.0 이하에서는 노티피케이션 채널을 만들어줄 필요가 없다.
            builder = NotificationCompat.Builder(this)
        }


        return builder
    }

 

5. Builder 생성 후 버튼을 누르면 Notification 오게 하는 예제.
        button4.setOnClickListener { view ->

            // 1. 노티피케이션 빌더 생성
            var builder = getNotificationBuilder1("channel2", "2번째 채널")

            // 2. 티커 생성
            builder.setTicker("Ticker")
            builder.setSmallIcon(android.R.drawable.ic_menu_search)

            //3. 비트맵객체 = 안드로이드에서 이미지를 객체로 만든 것.
            var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
            builder.setLargeIcon(bitmap)

            builder.setNumber(100)

            //사용자가 메세지를 클릭하면 자동 제거
            builder.setAutoCancel(true)

            builder.setContentTitle("Content Title")
            builder.setContentText("Content Text")

            // 4. 노티피케이션 생성
            var notification = builder.build()

            // 5. 노티피케이션 메시지를 관리하는 노티피케이션 매니저 생성.
            var mng = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            // 알림창에서 각 알림을 구분하는 id.
            mng.notify(20, notification)
        }

 

'Deperecated > Android_강의' 카테고리의 다른 글

안드로이드 - Style Notification  (0) 2020.02.14
안드로이드 - Pending Intent  (0) 2020.02.14
안드로이드 - 다이얼로그  (0) 2020.02.13
안드로이드 - 메시징 - Toast  (0) 2020.02.13
안드로이드 - ActionBar  (0) 2020.02.13