当前位置: 首页 > 知识库问答 >
问题:

启动前台的错误通知,服务通知的通道无效:

谭研
2023-03-14

所以,我有一个拥有50000用户的应用程序。最近我更新了我的android studio,把SDK编译成了27。我做了一些改变,以支持android 8的变化,包括通知的变化(例如,通知应该有唯一的渠道)。在Android One手机上测试了这个(运行android 8)。一切都很顺利。在更新playstore上的应用程序后,我在android 8设备上看到了这些崩溃(主要是谷歌Pixel手机)

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 defaults=0x0 flags=0x40 color=0xffffcf00 actions=3 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(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我在Crashlytics上看到了其中7个错误。每个都有相同的日志,大约10个用户有大约5000次崩溃。很明显,应用程序在循环中崩溃了。

我遇到的问题是,这些日志没有提供任何日志,从那里我可以看到我的应用程序中启动这个崩溃的来源。有什么办法能让我知道是哪个班搞的这场崩溃吗?有帮助吗?

此外,我还有以下通知代码:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notificationManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, name, importance);
                mChannel.setDescription(description);
                notificationManager.createNotificationChannel(mChannel);
            }
        }



    NotificationCompat.Builder builder = new NotificationCompat.Builder(context,id);
            builder.setContentTitle(pushMessages.size() + context.getString(R.string.new_notification))
                    .setContentIntent(pendingIntent)
                    .setContentText(pushMessages.get(0))
                    .setStyle(inboxStyle)
                    .setGroup(GROUP_KEY_NOTIFICATIONS)
                    .setGroupSummary(true)
                    .setAutoCancel(true);

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder.setSmallIcon(R.drawable.notification_img)
                        .setColor(context.getResources().getColor(R.color.settings_yellow));


            } else {
                builder.setSmallIcon(R.mipmap.ic_launcher);
            }
            summaryNotification = builder.build();
notificationManager.notify(i, summaryNotification);

共有3个答案

柯昆杰
2023-03-14

是的,有一种方法可以找出是哪个类抛出了这个错误。所以,首先你需要找出是哪个类在调用< code > service . start foreground 。可能是那个班级举办的。

使用Android Studio,转到<code>服务(或Mac上的<code>命令并单击方法签名。它将列出调用该方法的所有库中的类,包括文档引用。

我仍然面临同样的问题。我认为RoboSpice(现在已存档)正在抛出这些错误。找出答案的唯一方法是重构所有代码库以使用其他东西(Rx Retrofit可能)。

丁宏盛
2023-03-14

似乎您在8.1中必须创建自己的通知通道。

 private void startForeground() {
    String channelId ="";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createNotificationChannel("my_service", "My Background Service");
    }else{
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(PRIORITY_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(101, notification);
    }


}

之后呢

 @RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId ,String channelName){
    NotificationChannel chan = new NotificationChannel(channelId,
            channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(chan);
    return channelId;
}

更新:也不要忘记根据需要添加前台权限 Android P:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
屠锐
2023-03-14

您忘记设置通知的频道id。

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) builder.setChannelId(youChannelID);

if语句是可选的,取决于您的Min SDK级别。

 类似资料:
  • 所以我正在使用asmack库来侦听传入的xmpp数据包。我已经实现了服务。它启动,绑定,运行一个方法,然后解除绑定。该服务创建PacketListener以获取传入消息,并在新消息传入时显示通知。当应用程序处于活动状态时,这一切都起作用,但当它完全关闭(在窗口列表中滑动)时就不起作用了,它只是在运行服务,PacketListener不启动。但只要重新打开应用程序,它就会立即启动,关闭所有通知。我正

  • 编辑 我的通知是这样创建的,到目前为止,它在数千个API<26的android设备上工作:

  • 我创建了一个扩展服务并作为前台服务运行的类。我希望我的服务通知是持久的(即不通过刷卡删除)。但是,我的通知可以通过刷卡来撤销。 服务留档指出:...前台服务必须为状态栏提供通知,状态栏放在正在进行的标题下。这意味着除非服务停止或从前台删除,否则通知不能被驳回... 我确实设置了断点来检查是否命中了onDestroy()或stop Self(),但事实并非如此。该服务正在前台模式下运行,但我可以通过

  • 我正在尝试按以下方式启动前台服务: PS:我试了一个非模拟器的Android9.0设备,没有得到这个错误。

  • 我试过后台服务这样的代码,但第一次报警后应用程序崩溃了。并给出了这个错误: Android.app.remoteserviceException:StartForeground的错误通知:java.lang.runtimeException:服务通知的无效通道:通知(通道=null pri=0 contentview=null dibrate=null sound=null defaults=0x

  • null 我已经尝试调用NotificationManager.Cancel()和NotificationManager.CancelAll()。并尝试引发相同的原始PRIORITY_LOW通知。但它们都将通知图标留在状态栏中。