0

this is my notification code:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = "Ticker Text";
    long time = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, time);
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    Context context = getApplicationContext();
    CharSequence contentTitle = "Title";
    CharSequence contentText = "Text";
    Intent notificationIntent = new Intent(this, FistActiivty.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(1,notification);

How can I make the notification disappears only when user clicked on it , I mean I don't want the notification disappear when user clicks on clear button

thanks

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
mohamad bagheri
  • 499
  • 1
  • 10
  • 25
  • 1
    You can make it an ongoing notification. And implement an onClick for the notification through which you clear the same notification. – Anindya Dutta Jun 05 '15 at 04:15

2 Answers2

2

Use:

// This notification will not be cleared by swiping or by pressing "Clear all"
notification.flags |= Notification.FLAG_NO_CLEAR;

and also important:

.setOngoing(true) //put this inside your notification builder

or use: Notification.FLAG_ONGOING_EVENT; instead of .setOngoing(true)

This should help you out.

Strider
  • 4,452
  • 3
  • 24
  • 35
0

public static final int NOTIFICATION_ID = 1;

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

Intent myintent = new Intent(this, "your activity name"); ex:mainactivity.class

        myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);


        mBuilder.setAutoCancel(true); // this method is use for disappear notification after tap on it .

mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

anu
  • 213
  • 1
  • 3
  • 10