0

I have already create notification channel but still Crash at Android 8.1.0 Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification.

private void startForeground() {
    Log.d("mqttservice", "mqttservice startForeground");
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        Log.i("MqttService", " startForeground,  channelId == XP, channelName == XPMotors");
        try {
            final String channelId = "XP";
            String channelName = "XPMotors";
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setShowBadge(false);
            NotificationManager manager = (NotificationManager) MqttService.this.getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.createNotificationChannel(channel);
                NotificationChannel c = manager.getNotificationChannel("XP");
                //create失败时再次创建
                if (c == null) {
                    manager.createNotificationChannel(channel);
                }else{
                }
                c = manager.getNotificationChannel("XP");
                if (c != null) {
                    Notification notification = new NotificationCompat.Builder(MqttService.this, channelId)
                            .setContentTitle("小鹏汽车")
                            .setContentText("正在运行中")
                            .setSmallIcon(R.mipmap.app_icon)
                            .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
                            .setCategory(Notification.CATEGORY_SERVICE)
                            .build();
                    startForeground(100, notification);
                } else {//走这里的话 可能会anr
                    Log.d("mqttservice", "mqttservice stopself");
                }
            }
        } catch (Throwable e) {
            Log.e("MqttService", " Exception == " + e.toString());
        }
    }
}
linguokun
  • 41
  • 5
  • 2
    Please don't post screenshots of code, XML, or logcat output. Please post all text as text. – Mike M. Nov 02 '18 at 06:26
  • https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1 – AskNilesh Nov 02 '18 at 06:27
  • @NileshRathod I see the 8.1.0 source code if I set the targetSdkVersion as 27.It still crash with very low possibility although I create notification channel – linguokun Nov 02 '18 at 07:02

2 Answers2

0
// I will delete notification channel after 3 seconds
private void removeNotification() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.e("MqttService", " removeNotification 执行 ");
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
                try {
                    NotificationManager manager = (NotificationManager) MqttService.this.getSystemService(Context.NOTIFICATION_SERVICE);
                    if (manager != null) {
                        NotificationChannel channel = manager.getNotificationChannel("XP");
                        if (channel != null) {
                            manager.deleteNotificationChannel("XP");
                        }
                    }
                } catch (Throwable e) {
                    Log.e("MqttService", " Exception == " + e.toString());
                }
            }
        }
    }, 3000);
}
linguokun
  • 41
  • 5
0

make sure that you added attribute -> name=".the name of the class that you created channels in"

inside tag in manifest file.

Ali K.
  • 7
  • 4