개요
이 글에서는 Kotlin을 사용하여 안드로이드 앱 런처 아이콘에 알림 개수(배지, Badge)를 표시하는 방법을 단계별로 살펴봅니다.
알림 배지는 앱 아이콘 위에 표시되는 작은 숫자로, 읽지 않은 알림이 몇 개 쌓여 있는지 사용자가 한눈에 파악할 수 있게 해 주는 기능입니다. 다만 배지 표시 여부는 기기에 설치된 런처(Launcher) 앱이 이를 지원하는지에 따라 달라질 수 있으며, Android 8.0(오레오)부터는 알림 채널(Notification Channel) 생성이 필수라는 점도 함께 기억해 두세요.
1단계 — 새 프로젝트 만들기
Android Studio에서 File → New Project를 선택하여 새 프로젝트를 생성하고, 필요한 모든 항목을 입력해 프로젝트 설정을 완료합니다.
2단계 — 레이아웃 작성하기
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 화면 중앙에는 알림을 생성하는 버튼 하나를 배치했습니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="createNotification"
android:text="create notification" />
</RelativeLayout>
3단계 — MainActivity 코드 작성하기
src/MainActivity.kt 파일에 아래 코드를 추가합니다. 버튼을 누를 때마다 알림 카운트가 1씩 증가하며, setNumber()와 setBadgeIconType()을 통해 런처 배지에 표시될 숫자를 지정합니다. 또한 Android 8.0 이상에서는 알림 채널을 동적으로 생성하도록 처리했습니다.
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
class MainActivity : AppCompatActivity() {
var count = 0
private val channelId = "10001"
private val defaultChannelId = "default"
override fun onResume() {
super.onResume()
count = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
}
fun createNotification(view: View) {
count++
val notificationIntent = Intent(applicationContext, MainActivity::class.java)
notificationIntent.putExtra("fromNotification", true)
notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val builder = NotificationCompat.Builder(applicationContext, defaultChannelId)
builder.setContentTitle("My Notification")
builder.setContentIntent(pendingIntent)
builder.setContentText("Notification Listener Service Example")
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
builder.setAutoCancel(true)
builder.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
builder.setNumber(count)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(channelId,
"NOTIFICATION_CHANNEL_NAME", importance)
builder.setChannelId(channelId)
notificationManager.createNotificationChannel(notificationChannel)
}
notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
}
}
4단계 — 매니페스트 설정하기
androidManifest.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.q11">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
앱 실행하고 결과 확인하기
이제 애플리케이션을 실행해 결과를 확인해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 상단 툴바의 Run 아이콘
을 클릭하세요. 기기 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 앱의 기본 화면이 나타납니다.

버튼을 여러 번 눌러 알림을 생성하면, 런처의 앱 아이콘 위에 알림 개수가 배지 형태로 표시되는 것을 확인할 수 있습니다. 앱을 다시 열면 onResume()에서 카운트가 0으로 초기화되어 배지 숫자가 사라지는 동작도 함께 확인해 보세요.