android runtime_error ai_generated true

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

ID: android/notification-channel-required-api33

Also available as: JSON · Markdown · 中文
95%Fix Rate
89%Confidence
1Evidence
2023-09-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Android 13 (API 33) active
Android 12 (API 31) active
Android 8.0 (API 26) active

Root Cause

Foreground service notification requires a valid notification channel on API 26+, but channel is not created or ID mismatch.

generic

中文

前台服务通知在API 26+上需要有效的通知频道,但频道未创建或ID不匹配。

Official Documentation

https://developer.android.com/develop/ui/views/notifications/channels

Workarounds

  1. 95% success Create notification channel in Application.onCreate() or before service starts: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel("service_channel", "Service Channel", NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(channel) } Then use channel ID in NotificationCompat.Builder.
    Create notification channel in Application.onCreate() or before service starts: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel("service_channel", "Service Channel", NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(channel) } Then use channel ID in NotificationCompat.Builder.
  2. 90% success For services targeting API 33+, also request POST_NOTIFICATIONS permission at runtime: ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1) before startForeground.
    For services targeting API 33+, also request POST_NOTIFICATIONS permission at runtime: ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1) before startForeground.

中文步骤

  1. Create notification channel in Application.onCreate() or before service starts: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel("service_channel", "Service Channel", NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(channel) } Then use channel ID in NotificationCompat.Builder.
  2. For services targeting API 33+, also request POST_NOTIFICATIONS permission at runtime: ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1) before startForeground.

Dead Ends

Common approaches that don't work:

  1. Adding notification permission POST_NOTIFICATIONS in manifest without creating channel 70% fail

    Permission alone does not create channel; channel must be created programmatically before startForeground.

  2. Using default channel ID (e.g., 'default') without checking if it exists 65% fail

    Default channel may not be created; must explicitly create channel with createNotificationChannel.