안드로이드 개발자의 창고
[38일차 Android] Notification 본문
출처 : 안드로이드 앱스쿨 2기 윤재성 강사님 수업 PPT
📖 Notification
- 애플리케이션과 별도로 관리되는 메시지
- Notification 메시지를 OS에게 요청하면 OS는 알림 창 영역에 알림 메시지를 표시한다.
- 화면을 가지지 않는 실행단위에서 메시지를 표시할 때 주로 사용한다.
Notification의 특징
- 사용자가 메시지를 확인하거나 제거하기 전까지 메시지를 유지한다.
- 메시지를 터치하면 지정된 Activity가 실행되어 애플리케이션 실행을 유도할 수 있다.
Notification Channel
- 안드로이드 8.0 부터 새롭게 추가된 기능
- 이전에는 사용자가 설정에서 알림 메시지를 비활성화 하면 모든 메시지가 비활성화 되었다.
- 8.0 부터는 Notification Channel을 이용하여 알림 메시지를 채널이라는 그룹으로 묶어 관리할 수 있으며 사용사는 채널 별로 메시지 활성화 여부를 설정할 수 있다.
예제 코드
class MainActivity : AppCompatActivity() {
lateinit var activityMainBinding: ActivityMainBinding
// 확인할 권한
// 안드로이드 13버전 부터는 Notification 사용을 위해 POST_NOTIFICATIONS 권한을 사용자로부터 확인 받아야 한다.
val permissionList = arrayOf(
Manifest.permission.POST_NOTIFICATIONS
)
// Notification Channel을 코드에서 구분하기 위한 이름
val NOTIFICATION_CHANNEL1_ID = "CHANNEL1"
val NOTIFICATION_CHANNEL2_ID = "CHANNEL2"
// 사용자에게 노출 시킬 채널의 이름
val NOTIFICATION_CHANNEL1_NAME = "첫번째 채널"
val NOTIFICATION_CHANNEL2_NAME = "두번째 채널"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityMainBinding.root)
// 권한 확인 요청
requestPermissions(permissionList, 0)
// Notification Channel을 등록한다.
addNotificationChannel(NOTIFICATION_CHANNEL1_ID, NOTIFICATION_CHANNEL1_NAME)
addNotificationChannel(NOTIFICATION_CHANNEL2_ID, NOTIFICATION_CHANNEL2_NAME)
activityMainBinding.run {
button.setOnClickListener {
// Notification Builder를 가져온다.
val builder = getNoficationBuilder(NOTIFICATION_CHANNEL1_ID)
// 작은 아이콘
builder.setSmallIcon(android.R.drawable.ic_menu_search)
// 큰 아이콘
val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
builder.setLargeIcon(bitmap)
// 숫자 설정
builder.setNumber(100)
// 타이틀 설정
builder.setContentTitle("Content Title 1")
// 메시지 설정
builder.setContentText("Content Text 1")
// 메시지 객체를 생성한다.
val notification = builder.build()
// 알림 메시지르 관리하는 객체를 추출한다.
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// 메시지를 표시한다.
// 첫번째 매개변수에 넣어주는 정수는 단말기 전체에서 메시지를 구분하기 위한 값
// 값은 값으로 메시지를 계속 보여주면 메시지가 갱신 것이고
// 다른 값으로 메시지를 계속 보여주면 메시지가 각각 따로 나타난다.
notificationManager.notify(10, notification)
}
button2.setOnClickListener {
// Notification Builder를 가져온다.
val builder = getNoficationBuilder(NOTIFICATION_CHANNEL1_ID)
// 작은 아이콘
builder.setSmallIcon(android.R.drawable.ic_menu_search)
// 타이틀 설정
builder.setContentTitle("Content Title 2")
// 메시지 설정
builder.setContentText("Content Text 2")
// 메시지 객체를 생성한다.
val notification = builder.build()
// 알림 메시지르 관리하는 객체를 추출한다.
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// 메시지를 표시한다.
notificationManager.notify(20, notification)
}
button3.setOnClickListener {
// Notification Builder를 가져온다.
val builder = getNoficationBuilder(NOTIFICATION_CHANNEL2_ID)
// 작은 아이콘
builder.setSmallIcon(android.R.drawable.ic_menu_search)
// 타이틀 설정
builder.setContentTitle("Content Title 3")
// 메시지 설정
builder.setContentText("Content Text 3")
// 메시지 객체를 생성한다.
val notification = builder.build()
// 알림 메시지르 관리하는 객체를 추출한다.
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// 메시지를 표시한다.
notificationManager.notify(30, notification)
}
button4.setOnClickListener {
// Notification Builder를 가져온다.
val builder = getNoficationBuilder(NOTIFICATION_CHANNEL2_ID)
// 작은 아이콘
builder.setSmallIcon(android.R.drawable.ic_menu_search)
// 타이틀 설정
builder.setContentTitle("Content Title 4")
// 메시지 설정
builder.setContentText("Content Text 4")
// 메시지 객체를 생성한다.
val notification = builder.build()
// 알림 메시지르 관리하는 객체를 추출한다.
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// 메시지를 표시한다.
notificationManager.notify(40, notification)
}
}
}
fun addNotificationChannel(id:String, name:String){
// 안드로이드 8.0 이상일 때만 동작하게 한다.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
// 알림 메시지를 관리하는 객체를 추출한다.
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// id를 통해 NotificationChannel 객체를 추출한다.
// 채널이 등록된 적이 없다면 null을 반환한다.
val channel = notificationManager.getNotificationChannel(id)
// 채널이 등록된 적이 없다면...
if(channel == null){
// 채널 객체를 생성한다.
val newChannel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
// 단말기에 LED 램프가 있다면 램프를 사용하도록 설정한다.
newChannel.enableLights(true)
// LED 램프의 색상을 설정한다.
newChannel.lightColor = Color.RED
// 진동을 사용할 것인가?
newChannel.enableVibration(true)
// 채널을 등록한다.
notificationManager.createNotificationChannel(newChannel)
}
}
}
fun getNoficationBuilder(id:String) : NotificationCompat.Builder{
// 안드로이드 8.0 이상이라면
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val builder = NotificationCompat.Builder(this, id)
return builder
} else {
val builder = NotificationCompat.Builder(this)
return builder
}
}
}
- addNotificationChannel() : Notification Channel을 등록하는 메서드
- 첫 번째 매개 변수(id) : 코드에서 채널을 관리하기 위한 이름
- 두 번째 매개 변수(name) : 사용자에게 노출 시킬 이름
- getNoficationBuilder() : Notification 메시지 관리 객체를 생성하는 메서드
결과
앱의 Notification Channel
- 채널의 toggle을 끄면 해당 채널의 알림은 표시되지 않는다.
'Computer > Android' 카테고리의 다른 글
[38일차 Android] 다양한 Notification (0) | 2023.06.25 |
---|---|
[38일차 Android] Pending Intent (0) | 2023.06.25 |
[37일차 Android] Dialog (1) | 2023.06.23 |
[37일차 Android] SnackBar (0) | 2023.06.23 |
[37일차 Android] Toast (0) | 2023.06.23 |