startForeground的不良通知:java.lang.RuntimeException: invalid channel for service notification(无效通道)。

问题描述 投票:1回答:1

我收到了我的一些用户在Google Pixel 2、LG Nexus 5X和Nokia 6、7或8等Android 8+设备上的以下崩溃报告。我无法在使用8.0的LG V30、使用Android 9.0的Google Pixel或使用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)

在我的代码中,startForeground的唯一通知是在使用android.media.MediaPlayer播放音频时显示的通知。我怀疑这个通知是来自异常的通知,因为很多用户告诉我,MediaPlayer在他们的手机上无法工作--但我并不完全确定,因为崩溃日志并没有告诉这个错误到底发生在哪里。

创建这个通知的Kotlin代码是这样的。

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()
}

我使用Notification Channels来处理所有的通知,包括上面的那个。我无法重现这些通知的任何问题,因为它们似乎都能像预期的推送通知那样工作--而且大部分情况下也能用于前台服务。

我是否遗漏了什么?

android notifications foreground-service
1个回答
0
投票

'mysun'有一个观察,减少了我前台服务的投诉。

  • 系统找不到指定的资源
  • 这一般是由于 setSmallIcon(R.drawable.icon_small)在某些情况下,它还没有准备好(例如当你启动重启系统发送通知时),导致App异常。
  • 取而代之的是,从ApplicationInfo中获取应用程序的图标;例如 setSmallIcon(context.getApplicationInfo().icon)

参见mysun的 fatalerrors.org在此发布

© www.soinside.com 2019 - 2024. All rights reserved.