3

I like the ease of use of Notification.Builder but it seems not support the insistent mode of notifications.

Is there a way to set the flag FLAG_INSISTENT from the Notification.Builder?

Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69

2 Answers2

14

Not directly. There is a private setFlag() method on Notification.Builder -- I do not know why they did not expose it.

However, you can configure the rest of the Notification via a Builder, then adjust the flags on the completed Notification object.

Or, grab the code for Notification.Builder and modify it to create your own that exposes setFlag(), or adds setInsistent(), etc.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks, adjusting the flags on the completed Notification object is the workaround I'm using. I was wondering if there was a way to set the flag from the Notification.Builder but, apparently, it seems not. – Roberto Leinardi Jun 14 '12 at 13:29
  • FLAG_INSISTENT is not working in Oreo above.can you please suggest something? – Pooja Shukla Feb 29 '20 at 06:54
  • @PoojaShukla: If there is anything like `FLAG_INSISTENT`, it likely would be part of the notification channel, as that is where sounds are configured. However, I do not know of an option that would work like `FLAG_INSISTENT` used to. But, I have not researched the point much. – CommonsWare Feb 29 '20 at 12:17
4

Maybe my answer can be useful to someone else

You can use:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext());

builder.setContentTitle("Title")
       .setContentText("Hello world")
       .setSmallIcon(R.mipmap.ic_launcher);

Notification notification = builder.build();
notification.flags = Notification.FLAG_INSISTENT;
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(ID, notification)
Kiwi
  • 173
  • 2
  • 9