为什么在创建/更新通知时会收到RemoteServiceException?

and*_*per 8 android android-notifications android-8.1-oreo remoteserviceexception

背景

我有一个业余时间的应用程序,我已经工作了几年了(在这里),并且我尝试尽可能多地处理崩溃(我讨厌看到崩溃报告!)。

问题

最近发生的一次崩溃似乎很新:

android.app.RemoteServiceException: 
  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:6494)
  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)
Run Code Online (Sandbox Code Playgroud)

这就是我所看到的...

似乎仅适用于Android 8.1。由于该应用程序必须非常受欢迎,因此我确信它仅会在Android 8.1上出现。

我尝试过的

在互联网上搜索,我可以找到类似的崩溃,但是在所有崩溃中,它们都有更多线索。

因此,我试图回顾一下最近所做的更改。唯一的变化是将支持库迁移到Android-X。其余的更改很小,我认为它们不是造成更改的原因。

由于没有更多线索,我决定在此处报告有关Android-X的部分(如有需要,请在此处提供更多信息),因为我不认为这是因为我试图解决问题,并且看起来像是非常具体。

后来,我集成了Firebase Crashlytics,并在每个这样的日志上方获得了这个小提示:

Fatal Exception: android.app.RemoteServiceException
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel_id__app_monitor pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 category=service vis=SECRET)
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我确实正确打开了该服务,否则它将根本无法工作,这尤其奇怪,它仅在Android 8.1上发生,而不是在8.0上发生,这对如何操作有相同的要求启动前台服务。

这个有问题的通知是有关我用于全局监视与应用程序相关的事件的前台服务的通知(仅来自具有广播接收器限制的Android O)。更新了2种情况:

  1. 区域变更
  2. 在某个对话框上的某个复选框上打勾,该复选框会在某个时间点出现(单击该对话框时)。

当我测试了这两个功能后,它们在Android 8.0和8.1上都可以正常工作。

这是创建/更新通知的代码:

    @JvmStatic
    fun getUpdatedAppMonitorNotification(context: Context): Notification {
        val builder = Builder(context, context.getString(R.string.channel_id__app_monitor)).setSmallIcon(R.drawable.ic_stat_app_icon)//
                .setPriority(Notification.PRIORITY_DEFAULT).setCategory(Notification.CATEGORY_SERVICE)
        builder.setVisibility(NotificationCompat.VISIBILITY_SECRET)
        builder.setShowWhen(false)
        if (!PreferenceUtil.getBooleanPref(context, R.string.pref__avoid_showing_app_monitor_notification_dialog, false))
            builder.setContentTitle(context.getString(R.string.app_monitor__notification_title))
        if (VERSION.SDK_INT < VERSION_CODES.P) {
            val mainIntent = Intent(context, MainActivity::class.java)
                    .putExtra(MainActivity.EXTRA_OPENED_FROM_NOTIFICATION, true)
            builder.setContentIntent(PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT))
        } else {
            val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
                    .putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                    .putExtra(Settings.EXTRA_CHANNEL_ID, context.getString(R.string.channel_id__app_monitor))
            val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            builder.setContentIntent(pendingIntent)
        }
        return builder.build()
    }
Run Code Online (Sandbox Code Playgroud)

以及我在服务中使用的代码:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    NotificationId.initNotificationsChannels(this)
    val notification = getUpdatedAppMonitorNotification(this)
    startForeground(NotificationId.APP_MONITOR, notification)
    ...
    return super.onStartCommand(intent, flags, startId)
}
Run Code Online (Sandbox Code Playgroud)

这是我从其他地方调用的代码,用于更新通知:

    @JvmStatic
    fun updateAppMonitorNotification(context: Context) {
        if (VERSION.SDK_INT < VERSION_CODES.O)
            return
        val notification = AppMonitorService.getUpdatedAppMonitorNotification(context)
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(NotificationId.APP_MONITOR, notification)
    }
Run Code Online (Sandbox Code Playgroud)

问题

  1. 为什么会发生?关于Android X吗?通知构建有问题吗?

  2. 为什么只在Android 8.1上出现?

and*_*per 0

好的,我仍然不知道为什么会发生这种情况,但我发现的解决方法是让服务通过函数安全地调用它来完成其通知的所有更新context.startForegroundService

到目前为止,通过使用它,我仍然没有看到任何此类崩溃。

  • 你好,我遇到了同样的问题。目前我启动了我的 ForegroundService,然后是前台通知。它在 8.1 中工作得很好,但在 8.1 中不行。你能发布更详细的答案吗?你是如何修复它的?“让服务完成其通知的所有更新”到底是什么意思? (2认同)
  • 我解决了我的问题..:)如果我在`onStartCommand`中启动前台通知而不是在`onCreate()`中启动前台通知,我就摆脱了那个奇怪的错误...在`onCreate()`中启动通知没有任何问题虽然8.0.. (2认同)