startForeground的错误通知:java.lang.RuntimeException:服务通知的通道无效



我收到了以下崩溃报告,这些用户在Android 8+上的Google Pixel 2,LG Nexus 5X和诺基亚6,7或8等设备上。我无法在带有 8.0 的 LG V30、带有 Android 9.0 的谷歌像素或带有 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()
}

我正在使用通知渠道处理所有通知,包括上面的通知。我无法重现这些通知的任何问题,因为它们似乎都按预期用于推送通知 - 并且大部分也适用于前台服务。

我错过了什么吗?

'mysun' 有一个观察结果,减少了对我的前台服务的抱怨:

  • 系统找不到指定的资源
  • 这通常是由于setSmallIcon(R.drawable.icon_small)尚未准备好在某些情况下(例如,当您启动重新启动系统以发送通知时(,从而导致应用程序异常。
  • 相反,从应用程序信息中获取应用程序图标;例如setSmallIcon(context.getApplicationInfo().icon)

在此处查看Mysun的 fatalerrors.org 帖子

我遇到了类似的问题,事实证明我的上下文为空,然后我从.我的应用主类

PendingIntent broadcast = PendingIntent.getActivity
(context, 222, intent1, PendingIntent.FLAG_MUTABLE); 

一切都奏效了。

只是尝试找出您的上下文具有什么价值,也许您也有空

最新更新