HOME > android > tips

안드로이드 - invalid channel for service notificaiton 에러 해결 방법

JSFollow20 Jul 2019

Android에서 서비스를 foreground로 변경할 때 발생하는 "invalid channel for service notification" 에러 해결 방법입니다. startForeground()는 인자로 노티피케이션을 받는데요, 안드로이드 O부터 노티피케이션을 등록할 때 노티피케이션의 Channel Id를 먼저 등록해야 합니다. 만약 등록되지 않은 Channel Id로 노티피케이션을 등록하려고 하면, 아래와 같이 invalid channel for service notification에러가 발생합니다.

07-20 18:21:11.462  4055  4055 E AndroidRuntime: Process: com.codechacha.sample, PID: 4055
07-20 18:21:11.462  4055  4055 E AndroidRuntime: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=com.codechacha.sample pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0xc0 color=0x00000000 category=call vis=PRIVATE)
07-20 18:21:11.462  4055  4055 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
07-20 18:21:11.462  4055  4055 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:107)

채널 등록 및 startForeground

따라서 타겟 SDK가 안드로이드 O(API 26)라면 노티피케이션을 등록하기 전에 채널을 만들어야 합니다. 아래 코드는 노티피케이션 채널을 등록하는 코드입니다.

val channelId = "com.codechacha.sample1"
val channelName = "My service channel"
if (Build.VERSION.SDK_INT >= 26) {
    val channel = NotificationChannel(
        channelId, channelName,
        NotificationManager.IMPORTANCE_DEFAULT
    )
    var manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.createNotificationChannel(channel)
}

그 이후 노티피케이션을 생성하여 startForeground()를 호출하면 됩니다.

val notificationBuilder = NotificationCompat.Builder(this, channelId)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle("title")
    .setContentText("content text")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_CALL)
    .setFullScreenIntent(fullScreenPendingIntent, true)

val notification = notificationBuilder.build()
val NOTIFICATION_ID = 12345
startForeground(NOTIFICATION_ID, notification)

권한

startForeground()는 다음 권한을 필요로 합니다.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

권한이 없다면 다음과 같은 에러가 발생합니다.

07-20 18:33:13.952  4356  4356 E AndroidRuntime: java.lang.RuntimeException: Unable to create service com.codechacha.sample.MyService: java.lang.SecurityException: Permission Denial: startForeground from pid=4356, uid=10130 requires android.permission.FOREGROUND_SERVICE
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:3963)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.ActivityThread.access$1500(ActivityThread.java:219)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:107)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:214)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:7343)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=4356, uid=10130 requires android.permission.FOREGROUND_SERVICE
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.os.Parcel.createException(Parcel.java:2071)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:2039)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:1987)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6136)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.Service.startForeground(Service.java:707)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at com.codechacha.sample.MyService.onCreate(MyService.kt:55)
07-20 18:33:13.952  4356  4356 E AndroidRuntime: 	at android.app.ActivityThread.handleCreateService(ActivityThread.java:3951)