How can I make a notification permanent?

1

What I want is to make that while the app is open the notification does not close, the notification has a button to expand that is to close it.

To help me they have as an example the code of the notification:

Intent notificationIntent = new Intent(this, MainActivity.class); //<-- La clase que se abrira al hacer click en la notificacion
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
    notificationIntent.putExtra("notificationID", notificationID);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    CharSequence ticker ="Exit";
    CharSequence contentTitle = "App Open Mode";
    CharSequence contentText = "App Notification Open Mode";

    Notification noti = new NotificationCompat.Builder(this)
            .setContentIntent(pendingIntent)
            .setTicker(ticker)
            .setContentTitle(contentTitle)
            .setContentText(contentText)
            .setSmallIcon(R.mipmap.ic_launcher)
            .addAction(R.mipmap.ic_launcher, ticker, pendingIntent)
            .build();



    nm.notify(notificationID, noti);

I tried using this:

notif.flags = Notification.FLAG_ONGOING_EVENT;

But the app closes and does not work.

    
asked by Alejandro Matos 21.09.2017 в 06:08
source

2 answers

1

Hello, you can try this method.

builder.setOngoing(true);

greetings

    
answered by 21.09.2017 в 14:35
0

To make permanent and avoid dismissal (dismiss), it can be done using NotificationCompat.Builder defining the flag FLAG_ONGOING_EVENT :

notification.flags = Notification.FLAG_ONGOING_EVENT;

or define:

notification.setOngoing(true);
    
answered by 21.09.2017 в 19:03