android - 前台启动通知错误,服务通知 channel 无效 :

标签 android service notifications android-8.0-oreo android-8.1-oreo

因此,我在商店中有一个拥有 50000 名用户的应用程序。最近我更新了我的 android studio 并将 SDK 编译为 27。我做了一些更改以支持 android 8 更改,包括通知更改(例如通知应该具有唯一 channel )。在 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 次崩溃。所以,显然应用程序在循环中崩溃。

我遇到的问题是这些日志不提供任何日志,从那里我可以看到我的应用程序中引发此崩溃的来源。 有什么办法可以找出是哪个类(class)引发了这次崩溃?有帮助吗?

我还有这个通知代码:

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

最佳答案

您忘记为通知设置 channel ID。

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

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

关于android - 前台启动通知错误,服务通知 channel 无效 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587863/

相关文章:

android - 无法使用 crashlytics 构建 Android 应用程序

angular - 我如何将查询参数传递到 Angular 应用程序中服务内的 REST API?

swift - ios swift 3 addObserver 如何在点击推送通知时从应用程序中的任何位置重定向到特定 View

android - 如何在 Android O 中动态更改通知声音

android - Android Studio: list 合并未能为Sherlock操作栏添加AAR

Android - 警报管理器每天运行

android - 迁移到 Android 11 后无法绑定(bind)到服务

java - 无法解析符号 'Notification'

android - 在 CardView 中使用 ConstraintLayout

Java Axis 故障字符串