0

I am working on music application so i need to display notification on notification bar. for that i am using custom notification. The following is the code for notification for Android Oreo

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

        }

This code is working fine, but when swipe the notification from notification bar it will be removed. But my requirement is not remove when swipe the notification. I tried with setAutoCancel(true) and setAutoCancel(false). But not working it will be removed. So how to stick the notification here.

Please guide me how to do this.

Thanks In Advance

rams
  • 1,558
  • 7
  • 25
  • 48

2 Answers2

4

use setOngoing(true) to achieve this

Omer
  • 650
  • 5
  • 15
2

You have to add this setOngoing(true)

Try:

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();
touhid udoy
  • 4,005
  • 2
  • 18
  • 31