본문 바로가기
개발/Android Error

[Error] Fatal Exception: android.app.RemoteServiceException Bad notification for startForeground

by JhDroid 2021. 1. 5.
728x90

에러 메시지

Fatal Exception: android.app.RemoteServiceException: Bad notification for startForeground:
java.lang.RuntimeException: invalid channel for service notification: Notification

 

문제

  • Oreo(26) 버전부터 startForeground() 함수로 Notification을 띄울 때 Notification Channel을 생성하지 않고 Noti를 띄울 때 발생하는 에러
  • 작성자는 Notification Channel을 생성하고 있지만 해당 문제가 발생했음

 

해결

  • Notification Channel을 생성하고 있지 않다면
private val NOTIFICATION_CHANNEL_ID = "jhdroid_noti_channel"

private val manager =
    context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            NOTIFICATION_CHANNEL_ID,
            "channel_name",
            NotificationManager.IMPORTANCE_DEFAULT
        )
            manager.createNotificationChannel(channel)
    }
}
  • Notification Channel을 Application 클래스의 onCreate()에서 호출해 애플리케이션을 실행할 때 한 번 생성하거나 Notification을 띄울 때마다 호출해서 Channel을 생성해주어도 된다.

 

  • Notification Channel을 생성하고 있는데 동일한 에러가 발생한다면
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    builder = new Notification.Builder(context, channelId);
    builder.setChannelId(channelId);
} else {
    builder = new Notification.Builder(context);
}
  • Notification 생성 시 Builder에 setChannelId() 설정

 

 

* 글에 틀린 부분이 있으면 댓글 부탁드립니다 :D

728x90