简体   繁体   中英

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

Notification is displaying in android 11 ,you can customise this accordingly. I have tested this code and working fine in android 11 emulator.

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
                .setSmallIcon(playPauseBtn)
                .setLargeIcon(thumb)
                .setContentTitle("Title")
                .setContentText("Text")
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(imgUrl)
                        .bigLargeIcon(null))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification.setSmallIcon(playPauseBtn);
            notification.setColor(getResources().getColor(R.color.colorPrimary));
        } else {
            notification.setSmallIcon(playPauseBtn);
        }
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());

        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m,notification.build());

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM