[Android] RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification

👻·2022년 7월 28일
0

Android

목록 보기
7/11
post-thumbnail

📌 개요

기존의 안드로이드 버전들은 API 26버전 이하였는데, 지금 수정중인 버전은 API 28이다.

Notification을 사용 시 API 26 이상이면
Notification Channel을 등록해주어야 한다.

📌 해결방안

검색도 하기 전에 도움을 받아서 곧바로 해결했다.
또 문제가 생기면 참조할 포스트가 되겠다. 이상!

	@Override
    public void onCreate() {
        super.onCreate();

        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            createNotificationChannel();
            notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle(Setting.VIEWER_APP_NAME)
                    .setContentText("Execute Service")
                    .setWhen(System.currentTimeMillis())
                    .setChannelId(CHANNEL_ID)
                    .build();
        } else {
            notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle(Setting.VIEWER_APP_NAME)
                    .setContentText("Execute Service")
                    .setWhen(System.currentTimeMillis())
                    .build();
        }

        startForeground(1, notification);
        startForeground(1000, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)
    void createNotificationChannel() {
        @SuppressLint("WrongConstant")
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, Setting.VIEWER_APP_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(false);
        channel.setLightColor(Color.WHITE);
        channel.enableVibration(false);
        channel.setDescription("InfoFlex Updater Execute");

        NotificationManager manager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
    }
profile
Software Developer

0개의 댓글