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

1

I am receiving the following crash report from some of my users on devices like the Google Pixel 2, LG Nexus 5X and Nokia 6, 7 or 8 on Android 8+. I am not able to reproduce this crash on an LG V30 with 8.0, Google Pixel with Android 9.0 or emulators with 8.1.

Fatal Exception: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=-2 contentView=null vibrate=null sound=null smartAlertCount=0x0 defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6501)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

The only notification for startForeground in my code is a notification that is being displayed during audio playback with android.media.MediaPlayer. I suspect this notification to be the one from the exception, since many users tell me that the MediaPlayer is not working on their phone - but I am not totally sure, since the crash log is not telling exactly where this error occurs.

The Kotlin code for creating this notification looks like this:

class MediaPlayerService : Service() {

private var mediaPlayer: MediaPlayer? = null

override fun onBind(intent: Intent?): IBinder? = null

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    mediaPlayer = MediaPlayer()
    mediaPlayer?.let { mediaPlayer ->
        mediaPlayer.setOnPreparedListener(MediaPlayer.OnPreparedListener {
            mediaPlayer.start()
            showNotification()
        })
        try {
            mediaPlayer.setDataSource(URL)
            mediaPlayer.prepareAsync()
        } catch (exception: IOException) {
            exception.printStackTrace()
        }
    }
    return Service.START_NOT_STICKY
}

fun showNotification() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
        val channel = NotificationChannel(
                "channel_id",
                "Channel",
                android.app.NotificationManager.IMPORTANCE_LOW
        ).apply {
            lightColor = Color.GREEN
            enableVibration(true)
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            setSound(Settings.System.DEFAULT_NOTIFICATION_URI, null)
            setShowBadge(false)
        }
        notificationManager?.createNotificationChannel(channel)
    }

    NotificationCompat.Builder(context, "channel_id")
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setGroup("channel_id")
            .setGroupSummary(false)
            .setColor(Color.GREEN)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.icon_small)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.icon_large))
            .setContentTitle("Title")
            .setContentText("Text")
            .setTicker("Text")
            .setStyle(NotificationCompat.BigTextStyle().bigText("Text"))
            .setNumber(1)
            .build()
}

I am using Notification Channels for all of my notifications, including the one from above. I am not able to reproduce any problem with these notifications, as they all seem to work as expected for push notifications - and for the most part for the foreground service, too.

Is there something I am missing?

android
notifications
foreground-service
asked on Stack Overflow Aug 22, 2018 by Philipp Fahlteich • edited Aug 23, 2018 by Philipp Fahlteich

1 Answer

0

'mysun' had an observation that reduced the complaints for my foreground service:

  • The system can not find the specified resources
  • This is generally due to setSmallIcon(R.drawable.icon_small), which is not yet ready for some circumstances (such as when you start a restart system to send a notification), resulting in an App exception.
  • Instead, get the application icon from ApplicationInfo; e.g. setSmallIcon(context.getApplicationInfo().icon)

see mysun's fatalerrors.org post here

answered on Stack Overflow Apr 28, 2020 by ergohack

User contributions licensed under CC BY-SA 3.0