Ошибка startForeground в Android 8.1

112
13 января 2021, 14:40

Вот такая проблема: показывается список песен - нажимаю на на любую, для воспроизведения, показывает ошибку android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE) Как я понял проблема тут:

 @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
        Intent notIntent = new Intent(this, MainActivity.class);
        notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendInt = PendingIntent.getActivity(this, 0,
                notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder builder = new Notification.Builder(this);

            builder.setContentIntent(pendInt)
                    .setSmallIcon(R.drawable.play)
                    .setTicker(songTitle)
                    .setOngoing(true)
                    .setContentTitle("Playing")
                    .setContentText(songTitle);
            Notification not = builder.build();
            startForeground(NOTIFY_ID, not);
}

С языком Java знаком недавно

Answer 1

Начиная с Android Oreo при создании notification нужно указать chanelId в билдере:

NotificationCompat.Builder builder = new 
NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

Из документации:

Notice that the NotificationCompat.Builder constructor requires that you provide a channel ID. This is required for compatibility with Android 8.0 (API level 26) and higher, but is ignored by older versions.

https://developer.android.com/training/notify-user/build-notification#SimpleNotification

READ ALSO
Выдвигающаяся панель сбоку

Выдвигающаяся панель сбоку

Цель: Осуществить в приложении возможность выдвигать панель сбоку

136
Получить value из элемента листа JS [закрыт]

Получить value из элемента листа JS [закрыт]

Хотите улучшить этот вопрос? Обновите вопрос так, чтобы он вписывался в тематику Stack Overflow на русском

106