'Cannot show notification at lock screen Android Studio

I'm using NotificationCompat.Builder to show notification music player mini. I have been show success notfication but without at lock screen. I tried NotificationCompat.Builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC), NotificationCompat.Builder.setPriority(NotificationCompat.PRIORITY_MAX) but all them not worked. My device android version is 9.0.1. This is my code.

  private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Test";
            String description = "Đây là bản test";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("CHANNEL_ID", name, importance);
            channel.setShowBadge(true);
            channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager =  getContext().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

This is function call main.

        createNotificationChannel();
        // Create an explicit intent for an Activity in your app
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        Intent prevIntent = new Intent(this,NotificationReceiver.class).setAction("ActionPrev");
        Intent pauseIntent = new Intent(this,NotificationReceiver.class).setAction("ActionPause");
        Intent nextIntent = new Intent(this,NotificationReceiver.class).setAction("ActionNext");

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        PendingIntent prevpendingIntent = PendingIntent.getBroadcast(this,0,prevIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pausependingIntent = PendingIntent.getBroadcast(this,0,pauseIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent nextpendingIntent = PendingIntent.getBroadcast(this,0,nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        if(mangbaihat.get(position).getFields().getImage_Url().contains("/storage/emulated/0/Download")){
            final BitmapFactory.Options options = new BitmapFactory.Options();
            picture = BitmapFactory.decodeFile(mangbaihat.get(position).getFields().getImage_Url(), options);
        }else{
            picture = getBitmapFromURL(mangbaihat.get(position).getFields().getImage_Url());
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHANNEL_ID")
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setSmallIcon(R.drawable.logo)
                .setLargeIcon(picture)
                .setContentTitle(mangbaihat.get(position).getFields().getMusic_Title())
                .setContentText(mangbaihat.get(position).getFields().getComposed())
//                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .addAction(R.drawable.iconpreview_mini,"Previous",prevpendingIntent)
                .addAction(R.drawable.iconpause_mini,"Pause",pausependingIntent)
                .addAction(R.drawable.iconnext_mini,"Next",nextpendingIntent)
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle().
                    setShowActionsInCompactView(1 /* #1: pause button */).
                    setMediaSession(mediaSession.getSessionToken()))
            .setPriority(NotificationCompat.PRIORITY_MAX)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(0, builder.build());

Pls,Can anyone help me! Thank you so much!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source