首页 > 解决方案 > 启动前台的错误通知,服务通知的无效通道:

问题描述

所以,我在商店里有一个拥有 50000 个用户的应用程序。最近我更新了我的 android studio 并将 SDK 编译到 27。我做了一些更改以支持 android 8 更改,包括通知更改(例如通知应该有唯一的通道)。在 Android One 手机上进行了测试(运行 android 8)。一切都很顺利。在 Playstore 上更新应用程序后,我在 android 8 设备(主要是 Google 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);

标签: androidservicenotificationsandroid-8.0-oreoandroid-8.1-oreo

解决方案


您忘记为通知设置频道 ID。

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

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


推荐阅读