RemoteServiceException: Bad notification for startForeground...

原文链接: https://blog.csdn.net/dandelionela/article/details/86092293

写了一个前台通知,在Android5.0设备上测试没问题,换到8.0之后报错:


 
 
  1. android.app.RemoteServiceException: Bad notification for startForeground:
  2. java.lang.RuntimeException: invalid channel for service notification:
  3. Notification(channel= null pri= 0 contentView= null vibrate= null sound= null defaults= 0x0 flags= 0x40 color= 0x00000000 vis= PRIVATE)

查看资料:https://blog.csdn.net/misiyuan/article/details/78384819

原来是Android8.0对通知做了细分,引进了channel,这主要影响两个方面:
A。普通通知Notification;B。前台服务通知

而我遇到的就是后者。

A.普通通知Notification:

对于普通通知,对于Android 8.0的适配可参照:https://blog.csdn.net/qq_34773981/article/details/78173376

主要相对于传统的写法修改两处:

①在“NotificationManager”中设置添加“NotificationChannel”属性:


 
 
  1. NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  2. // 【适配Android8.0】给NotificationManager对象设置NotificationChannel
  3. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  4. NotificationChannel channel = new NotificationChannel( "notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
  5. notificationManager.createNotificationChannel(channel);
  6. }

②在“Notification”中使用“setChannelId()”方法设置添加“channel_id”属性:


 
 
  1. NotificationCompat.Builder builder = new NotificationCompat.Builder( this);
  2. ...
  3. ...
  4. // 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
  5. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  6. builder.setChannelId( "notification_id");
  7. }
  8. ...
  9. builder.build();

其中,第①步骤中的“NotificationChannel”的构造函数中参数列表依次为:
【channel_id】唯一id,String类型;
【channel_name】对用户可见的channel名称,String类型;
【importance_level】通知的重要程度。

importance_level主要有七种层次:

IMPORTANCE_NONE:      (0)关闭通知。在任何地方都不会显示,被阻塞
IMPORTANCE_MIN:          (1)开启通知。不弹出,不发提示音,状态栏中无显示
IMPORTANCE_LOW:        (2)开启通知。不弹出,不发提示音,状态栏中显示
IMPORTANCE_DEFAULT:(3)开启通知。不弹出,发出提示音,状态栏中显示【默认】
IMPORTANCE_HIGH:       (4)开启通知。会弹出,发出提示音,状态栏中显示

IMPORTANCE_MAX:        (5)开启通知。会弹出,发出提示音,可以使用full screen intents(比如来电)。重要程度最高
IMPORTANCE_UNSPECIFIED:(-1000)表示用户未设重要值。该值是为了持久的首选项,且永不应该与实际通知相关联

 该部分参考:
https://blog.csdn.net/Haienzi/article/details/81268022    【标题:Android8.0使用通知创建前台服务】
https://www.jianshu.com/p/f85ef58edf63    【标题:Android Oreo 通知新特性,这坑老夫先踩了】

前世今生:

Android O之前,叫通知“优先级”,通过在Build时,setPriority() 设置,共分为5档(-2 ~ 2);
默认值:Notification.PRIORITY_DEFAULT

Android O之后,叫通知“重要性”,通过NotificationChannel的 setImportance() 设置,也是5档(0 ~ 4);
默认值:NotificationManager.IMPORTANCE_DEFAULT

即使你设置了通知声音、震动这些属性,其“重要性”也必须满足下表对应的档位:

ImportanceBehaviorUsageExamples
NONEDon't show in the shadeNormally, Suppressing notification from package by user requestBlocked apps notification
MINNo sound or visual interruptionNon-essential information that can wait or isn’t specifically relevant to the userNearby places of interest, weather, promotional content
LOWNo soundNotification channels that don't meet the requirements of other importance levelsNew content the user has subscribed to, social network invitations
DEFAULTMakes a soundInformation that should be seen at the user’s earliest convenience, but not interrupt what they're doingTraffic alerts, task reminders
HIGHMakes a sound and appears on screenTime-critical information that the user must know, or act on, immediatelyText messages, alarms, phone calls



 

该部分参考: https://www.jianshu.com/p/99bc32cd8ad6    【标题:NotificationChannel 适配填坑指南】

B.前台服务通知:

与前者需要添加的代码相同。

前台服务可通过【startForeground()】方法在通知栏列出前台服务的通知,在传统的写法中类似于普通通知Notification的写法,但是不需要使用NotificationManager来管理显示通知。但是在Android8.0之后引入NotificationChannel之后,即使找不到NotificationManager的使用位置,也是要把【A.普通通知Notification---->①在“NotificationManager”中设置添加“NotificationChannel”属性】中的代码在执行【startForeground();】语句之前写出来。


 
 
  1. Intent intent = new Intent( this, TestActivity.class);
  2. PendingIntent pi = PendingIntent.getActivity( this, 0, intent, 0);
  3. NotificationCompat.Builder builder = new NotificationCompat.Builder( this);
  4. builder.setSmallIcon(R.drawable.ic_launcher);
  5. builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
  6. ...
  7. ...
  8. // 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
  9. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  10. builder.setChannelId( "notification_id");
  11. }
  12. ...
  13. ...
  14. // 额外添加:
  15. // 【适配Android8.0】给NotificationManager对象设置NotificationChannel
  16. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  17. NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  18. NotificationChannel channel = new NotificationChannel( "notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
  19. notificationManager.createNotificationChannel(channel);
  20. }
  21. // 启动前台服务通知
  22. startForeground( 1, builder.build());

参见:https://blog.csdn.net/Haienzi/article/details/81268022

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值