为什么创建/更新通知时会出现RemoteServiceException异常?

12

背景

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

问题

最近出现了一个似乎很新的崩溃,就是这个:

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)

这就是我所见的一切...

似乎只在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)

这很奇怪,因为我正确地打开了服务,否则它根本无法工作。特别奇怪的是,这只会在Android 8.1上发生,而不会在例如相同要求如何启动前台服务的8.0上发生。

有问题的通知是我用来全局监视与应用有关的事件(仅从Android O开始,该版本存在BroadcastReceivers的限制)的前台服务。它在两种情况下更新:

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

我在服务中使用的代码:

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

以下是我从其他地方调用以更新通知的代码:

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

这些问题

  1. 它为什么会发生?是与Android X有关吗?还是通知构建出了问题?

  2. 为什么它只在Android 8.1上发生?


有没有这样的情况,你可能会尝试在没有定义通道的情况下显示通知?你是否意外为R.string.channel_id__app_monitor提供了翻译,比如说你使用不同的名称创建了通道,然后尝试根据语言环境更改使用它? - CommonsWare
在显示通知之前,我总是会准备好通道。请查看“initNotificationsChannels”,并且它也会在应用程序类的onCreate中启动服务时进行初始化。如果我尝试过这个,我想我会得到不同的异常,不是吗?channel_id__app_monitor没有被翻译,并且也被标记为未翻译。 - android developer
@CommonsWare我已经决定不直接更新通知,而是始终启动服务,并让它为我更新通知。到目前为止,似乎已经解决了这个问题。希望这确实是一个好的解决方法。 - android developer
2个回答

0

好的,我仍然不知道为什么会发生这种情况,但是我找到了一个解决方法,就是通过安全地调用context.startForegroundService函数,让服务执行其所有通知的更新。

到目前为止,我使用这个方法后,仍然没有看到任何这样的崩溃。


2
嗨,我遇到了同样的问题...目前我启动了我的ForegroundService,然后跟着前台通知...在8.1中它运行得很好,但在8.1中不行...你能发表更详细的答案,说明你是如何解决这个问题的吗?你所说的“让服务更新其通知”的意思是什么? - mboy
2
我解决了我的问题.. :) 如果我在onStartCommand中启动前台通知而不是在onCreate()中启动,就可以摆脱那个奇怪的错误了...虽然在onCreate()中启动通知没有8.0的任何问题。 - mboy
我也遇到了同样的错误。Android开发者,您能否详细说明一下通过context.startForegroundService调用是什么意思? - N0000B
@N0000B 我在两个上设置了,并检查一个字段是否已经初始化。 - android developer
我认为错误是由于重新创建通知渠道/通知引起的。我已经添加了一个标志来判断服务是否已经在运行,只有在它没有运行时才启动它。自从我几天前做出更改后,还没有看到这个错误(祈祷不要再出现)。 - N0000B
显示剩余2条评论

0

我认为这就是问题所在:

.putExtra(Settings.EXTRA_CHANNEL_ID, context.getString(R.string.channel_id__app_monitor))

您需要输入(不变的)频道ID,而不是本地化的频道名称

如果您能够重现此问题,您还可以在您的错误报告中查找 AppSettings.*[您的应用程序],并查看是否有一个与您尝试使用的频道ID匹配的频道。


崩溃报告是通过Crashlytics自动报告的,而不是由我报告。此外,你所写的是不正确的。该字符串没有被翻译。并非所有字符串都应该被翻译。你可以将一个字符串标记为“未翻译”,也可以将字符串放在不在“strings.xml”文件中的文件中。 - android developer
让我再检查一遍。 - Prags

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接