在 Android Oreo 中禁用滑动删除通知

Disable swipe removal notification in Android Oreo

我正在开发音乐应用程序,所以我需要在通知栏上显示通知。为此,我正在使用自定义通知。以下是AndroidOreo

的通知代码
String NOTIFICATION_CHANNEL_ID = "001";
        RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.mynotification);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setDescription("Channel description");

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
            notificationBuilder.setSmallIcon(R.drawable.exo_edit_mode_logo)
                    .setCustomContentView(notificationLayout)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_DEFAULT);

            NotificationManagerCompat com = NotificationManagerCompat.from(this);
            com.notify(001, notificationBuilder.build());

        }

这段代码工作正常,但是当从通知栏滑动通知时,它将被删除。但是我的要求是滑动通知时不会删除。 我尝试使用 setAutoCancel(true)setAutoCancel(false)。但不工作它将被删除。那么这里怎么贴通知呢

请指导我如何操作。

提前致谢

你必须添加这个 setOngoing(true)

尝试:

Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("Text")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(false)
                .setOngoing(true);

Notification notification = builder.build();

使用setOngoing(true)实现这个