Android 알림 스타일

timothy jeong·2021년 11월 5일
0

Android with Kotlin

목록 보기
20/69

알림에 보이는 정보는 기본으로 문자열이지만 그 밖에 여러 가지 스타일을 제공한다. 이 스타일을 이용하면 문자열 이외에 다양한 콘텐츠로 알림을 구성할 수 있다.

큰 이미지 스타일

알림에 큰 이미지를 출력할 때는 BigPictureStyle 을 이용한다. 우리가 보통 사진을 캡쳐했을때 알림으로 뜨는게 큰 이미지 스타일이다.

val bigPicture = BitmapFactory.decodeResource(resources, R.drawable.{some_big_picture})
val bigStyle = NotificationCompat.BigPictureStyle()
bigStyle.bigPicture(bigPicture)

notificationBuilder.setStyle(bigTextStyle)

긴 텍스트 스타일

알림에 긴 문자열을 출력해 사용자가 앱을 실행하지 않아도 많은 정보를 알 수 있게 할 수 있다. 대표적으로 이메일 앱에서 수신한 이메일이 제목과 발신자 뿐만 아니라 이메일의 일부 내용도 보여준다.

val bigTextStyle = NotificationCompat.BigTextStyle()
bigTextStyle.bigText(resources.getString(R.string.long_text))

notificationBuilder.setStyle(bigStyle)

상자 스타일

하나의 알림에 문자열을 여러개 나열할 때 유용하다. InboxStyle 을 이용한다.

val style = NotificationCompat.InboxStyle()
style.addLine("1코스 : ")
style.addLine("2코스 : ")
style.addLine("3코스 : ")
style.addLine("4코스 : ")

notificationBuilder.setStyle(style)

메시지 스타일

메시지 스타일은 여러 사람이 주고받은 메시지를 구분해서 출력할때 사용한다. 알림에 보일 각 메시지마다 Message 객체로 표현한다.

Message(text: CharSequence, timestamp: Long, sender: Person
하나의 Message 객체는 3가지 정보로 표현된다. 메시지 내용, 메시지 발생 시각, 그리고 보낸 사람을 나타내는 객체 Person 이다. 호환성 문제로 androidx.app 의 Person 객체가 아닌 androidx.core.app 의 Person 객체를 Import 해야한다.

val sender1: androidx.core.app.Person = androidx.core.app.Person.Builder()
            .setName("Tom")
            .setIcon(IconCompat.createWithResource(this, android.R.drawable.btn_plus))
            .build()

        val sender2: androidx.core.app.Person = androidx.core.app.Person.Builder()
            .setName("Jane")
            .setIcon(IconCompat.createWithResource(this, android.R.drawable.btn_plus))
            .build()

        val sender3: androidx.core.app.Person = androidx.core.app.Person.Builder()
            .setName("Sam")
            .setIcon(IconCompat.createWithResource(this, android.R.drawable.btn_plus))
            .build()

이렇게 만든 Person 객체를 Message에 임포트해서 이용한다.

val now = System.currentTimeMillis()
val message1 = NotificationCompat.MessagingStyle.Message("Hello", now, sender1)
val message2 = NotificationCompat.MessagingStyle.Message("WHAT?", now + 500, sender2)
val message3 = NotificationCompat.MessagingStyle.Message("lol~", now + 1000, sender3)

val messageStyle = NotificationCompat.MessagingStyle(sender1)
        .addMessage(message1)
        .addMessage(message2)
        .addMessage(message3)

notificationBuilder.setStyle(messageStyle)

profile
개발자

0개의 댓글