javaandroidkotlinnotifications

Notification does not appear in Android 11 (sdk 30)


Notification worked fine, appeared (sdk 28, android 9). I decided to try it on the emulator for android 11, as a result, notifications do not appear. I can't understand my mistake. Works everywhere, tested on Xiomi Redmi 7 and Pixel 4 under sdk 28. And on the Nexus 5 under sdk 30, the application works, but only without notifications.

private void createNotificationChannel(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel1 = new
                    NotificationChannel(CHANNEL_ID_1, "Channel(1)", NotificationManager.IMPORTANCE_HIGH);
            channel1.setDescription("Channel 1 Dec..");

            NotificationChannel channel2 = new
                    NotificationChannel(CHANNEL_ID_2, "Channel(2)", NotificationManager.IMPORTANCE_HIGH);
            channel2.setDescription("Channel 2 Dec..");

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel1);
            notificationManager.createNotificationChannel(channel2);
        }
    }

void showNotification(int playPauseBtn){
        
        ...

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
                .setSmallIcon(playPauseBtn)
                .setLargeIcon(thumb)
                .setContentTitle(musicFiles.get(position).getTitle())
                .setContentText(musicFiles.get(position).getArtist())
                .addAction(R.drawable.ic_skip_previous, "Previous", prevPending)
                .addAction(playPauseBtn, "Pause", pausePending)
                .addAction(R.drawable.ic_skip_next, "Next", nextPending)
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                        .setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(contentPending)
                .setDeleteIntent(deletePending)
                .setOnlyAlertOnce(true)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .build();
        startForeground(2, notification);
    }

Solution

  • Thanks for the answer, but the error turned out to be in another. This is in the style of - .setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(new MediaSessionCompat(getBaseContext(), TAG).getSessionToken()))

    You can't do that (it will only take you up to 11 android)! You need to do this: .setStyle(new androidx.media.app.NotificationCompat.MediaStyle())