안드로이드 OS 10부터 특히 삼성 단말에서는 알림을 24개까지만(삼성 단말 기준) 노출할 수 있고, 그 이후에 생성된 알림은 무시되는 문제가 발생할 수 있다.
이를 해결하기 위해 가장 오래된 알림부터 순차적으로 삭제하면서 새로운 알림을 보여주는 방법을 공유한다.
문제 상황
알림이 24개를 초과하게 되면 새롭게 생성되는 알림은 사용자에게 표시되지 않는다.
이는 새롭게 발생되어야 하는 알림이 사용자에게 전달되지 않는 문제를 야기할 수 있다.
해결 방법
가장 오래된 알림부터 삭제하고 새로운 알림을 추가하는 방법으로 해결해보려고 한다.
이를 위해 알림 ID를 관리하고, 알림을 생성할 때마다 알림 ID를 순환시키는 방식으로 오래된 알림을 삭제한다.
구현 방법
1. 알림 ID 관리
알림 ID를 관리하기 위해 큐(Queue) 자료 구조를 사용할 수 있다.
새 알림을 추가할 때마다 큐에 알림 ID를 추가하고, 알림 개수가 24개를 초과하면 가장 오래된 알림 ID를 제거한다.
2. NotificationManager를 통한 알림 관리
NotificationManager를 사용하여 알림을 생성하고 제거할 수 있다. 각 알림에는 고유한 ID를 부여하여 관리한다.
3. 예제 코드
다음은 알림을 생성하고, 알림 갯수가 24개를 초과하면 가장 오래된 알림을 제거한다.
class NotificationHelper(private val context: Context) {
private val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val notificationQueue: Queue<Int> = LinkedList()
private val maxNotifications = 24
companion object {
const val CHANNEL_ID = "default_channel"
}
init {
createNotificationChannel()
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Default Channel"
val descriptionText = "This is the default notification channel."
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
notificationManager.createNotificationChannel(channel)
}
}
fun showNotification(title: String, message: String) {
val notificationId = System.currentTimeMillis().toInt()
if (notificationQueue.size >= maxNotifications) {
val oldestNotificationId = notificationQueue.poll()
notificationManager.cancel(oldestNotificationId!!)
}
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager.notify(notificationId, notification)
notificationQueue.add(notificationId)
}
}
그다음 알람을 발생시킬 주체에서 NotificationHelper를 생성하고 show 메서드를 호출하면 된다.
꼭 Helper 클래스로 정의하지 않더라도 본인의 프로젝트 스타일에 맞도록 어느정도 커스텀이 필요할 것 같다.
class MainActivity : AppCompatActivity() {
private lateinit var notificationHelper: NotificationHelper
private lateinit var binding: MainActivityBinding //구현부 생략
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationHelper = NotificationHelper(this)
binding.showBtn.setOnClickListener {
notificationHelper.showNotification("New Notification", "This is a new notification.")
}
}
}
위 방법을 통해 안드로이드 OS 10 이상의 삼성 단말에서 알림 갯수 제한 문제를 해결했다.
오래된 알림을 순차적으로 삭제하여 새로운 알림이 항상 표시되도록 하여 중요한 알림이 사용자에게 전달되지 않는 문제를 방지할 수 있다.
'Android' 카테고리의 다른 글
[Android] 시스템 설정 변경 처리하기 (0) | 2024.06.25 |
---|---|
[Android] ViewModel에서 LiveData와 StateFlow의 권장 사용 방법 (0) | 2024.06.20 |
[Android] DiffUtil과 Payload를 활용한 RecyclerView 성능 최적화 (0) | 2024.06.18 |
[Android] RecyclerView 성능 최적화를 위한 ListAdapter 살펴보기 (0) | 2022.12.22 |
[Android] Notification 커스텀하기 (0) | 2022.12.21 |