1

i want to create a notification that user can't close it by drag to left or right. user must click o that to close notification

my code is :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification n  = new Notification.Builder(getApplicationContext())
                     .setContentTitle("title")
                     .setContentText("text")
                     .setSmallIcon(R.drawable.ic_tt)
                     .setLargeIcon( BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_tt))
                     .setContentIntent(pIntent)
                     .setDefaults(Notification.DEFAULT_SOUND)
                     .setOngoing(true).build();
                     n.flags |= Notification.FLAG_ONGOING_EVENT;

    NotificationManager notificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, n);

but it dosnot work any idea ?

mohsen barati
  • 73
  • 2
  • 8

1 Answers1

1

You need to use both functions to retain the notification,

nBuilder.setOngoing(true);
nBuilder.setAutoCancel(true);

It can be cancelled only when user taps on it. Find setAutoCancel in this Managing Notifications. Hope this helped!

Harin
  • 2,413
  • 3
  • 15
  • 30