Android Crash:Bad notification for startForeground

Android 8.0开始Notification 需要指定一个channel,当target大于26时,这个channel需要进行系统注册,否则会crash,crash信息如下:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=default pri=0 contentView=null vibrate=null sound=null 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:6494)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

修改方案

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  val channelId = "default"
  val channel = NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT)
  val nm = service.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
  nm?.let {
    if (it.getNotificationChannel(channelId) == null) {//没有创建
      it.createNotificationChannel(channel)//则先创建
    }
  }
  val notification: Notification
  val builder = Notification.Builder(service, channelId)
  .setContentTitle("")
  .setContentText("")
  notification = builder.build()
  service.startForeground(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
}


private void createNotificationChannel(NotificationManager manager) {
        if (manager.getNotificationChannel("default") == null) {
            NotificationChannel notificationChannel =
                    new NotificationChannel("default",
                            "notification_channel",
                            NotificationManager.IMPORTANCE_LOW);

            notificationChannel.setDescription(
                    "notification_channel_description");

            manager.createNotificationChannel(notificationChannel);
        }
    }

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: android.app.remoteserviceexception: bad notification for startforeground是一个异常,通常是由于在启动前台服务时,通知的参数不正确导致的。这个异常通常会在Android应用程序中出现,需要检查代码中的通知参数是否正确,以确保启动前台服务时不会出现异常。如果您需要更多的帮助,请提供更多的上下文信息,以便我们更好地理解您的问题。 ### 回答2: Android.app.remoteserviceexception是一种异常,通常在通过启动前台服务时发生。在Android系统中,前台服务比后台服务更重要,因为它们能够向用户显示通知,使用户了解到正在运行的服务,同时确保系统不会在资源紧张时关闭它们。启动前台服务时,应该向系统提供可让用户了解服务正在运行的通知。 当出现“bad notification for startforeground”异常时,通常表示通知无效或不可用。这可能是因为通知没有正确地设置,或者可能是因为通知复制失败,例如,如果通知超过了允许的大小限制。如果通知无效,它将无法在前台服务启动后正确地显示给用户。 要解决此异常,您需要检查通知的正确性。首先,您需要确保通知具有必要的元素,例如标题和内容。其次,您需要确保正确设置通知的id,以确保通知可以正常显示和更新。还可以检查通知是否越过了允许的大小限制,导致通知无效。 此外,您可以使用日志来检查异常的详细信息,并确定导致该问题的根本原因。通过日志,您可以查看哪些操作导致了异常,以及异常的位置。这可以帮助您快速解决问题,并确保您能够提供正常运行的前台服务。 总之,在启动前台服务时,您应该充分了解通知的重要性,并确保它们得到正确的设置。如果遇到bad notification for startforeground异常,您应该检查通知是否有效和正确设置,以确保正常运行您的前台服务。 ### 回答3: 在 Android 应用程序开发中,有时候会遇到 android.app.remoteserviceexception: bad notification for startforeground 错误。这个错误通常是因为通知栏的设置不正确,而导致的崩溃异常。 在 Android 系统中,使用 Foreground Service 进行长时间运行的任务,需要在通知栏中创建一个持久的通知,来告知用户当前正在后台运行的任务。在创建通知时,需要注意以下几点: 1. 通知的 Channel ID 要正确设置,与 Notification Channel 一致。 2. 通知的 Icon 要正确设置,不能为 0。 3. 通知的 Title 和 Content 要正确设置,以便用户能够理解当前后台运行的任务。 如果通知栏的设置不正确,就可能会引起 android.app.remoteserviceexception: bad notification for startforeground 错误。通常这个错误会在 startForeground 方法被调用时出现,导致应用程序崩溃。 为了解决这个问题,我们可以检查通知栏的设置是否正确,尤其是 Channel ID、Icon、Title 和 Content 等参数。同时,也可以查看日志文件,找出造成错误的具体原因,再进行相应的修复。另外,在 Android 8.0 及以上的版本中,还需要注意是否已经创建了 Notification Channel。如果没有创建 Notification Channel,也会导致类似的错误。 总的来说,android.app.remoteserviceexception: bad notification for startforeground 错误是由于通知栏的设置不正确,而导致的崩溃异常。对于开发者来说,需要仔细检查通知栏的设置,确保各个参数都正确无误,避免出现类似的错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值