2

I am trying to fix four buttons always at the top of notification bar that cannot be closed. The buttons will be fixed all the time when the application is launched for the first time. For this purpose I am creating notification through Notification Builder as follows.

 Notification notification = new Notification.Builder(this).build();      
       notification.flags = Notification.FLAG_ONGOING_EVENT;
       notification.icon = R.drawable.ic_launcher;
       notification.contentIntent = pendingIntent;
       notification.setSmallIcon = R.drawable.ic_launcher;
       notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
       notification.priority = Notification.PRIORITY_HIGH;
       mNotificationManager.notify(Constants.NOTIFICATION_ID_BRANDING, notification);

Everything works fine and I could even able to show my notification on top but whereas while I receive some other notification from different application my notification is showing below that application.

The line notification.priority = Notification.PRIORITY_HIGH; does the thing but it is specific only for our application.

My Requirement: But my requirement is to keep my notification always on the top irrespective of any notification. Please help me with your tips and solutions. Thanks in advance.

Chandru
  • 5,954
  • 11
  • 45
  • 85

2 Answers2

1

I am trying to fix four buttons always at the top of notification bar that cannot be closed

That is not going to be possible, unless you create your own custom ROM.

First, Android, not you, determines the order of the items in the notification tray.

Second, on Android 5.0+, the user can block notifications, thereby preventing your app from showing anything, let alone something at the top.

Third, if 2+ programmers try implementing the same "requirement", one has to lose, because there is only one top-most item. Hence, even if your approaches were working, they would possibly fail in the face of some other developer trying the same thing.

Beyond that, even if you come up with some script-kiddie approach that seems to work, it may fail on other devices, as the rules for ordering notifications can be device- and OS-version-dependent.

while I receive some other notification from different application my notification is showing below that application

That is because Android, not you, controls the order of notifications.

The line notification.priority = Notification.PRIORITY_HIGH; does the thing but it is specific only for our application.

More accurately, it is merely one criterion for the order. Android is welcome to use other criteria (e.g., time, system app vs. ordinary app, ongoing vs. regular, foreground service vs. regular) to decide what order it will use.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your reply. Even I am wandering the same so. But whereas in applications like Clean Master they are doing this thing by pinning their notification thing to appear all the time at the top. But again it is not a default behaviour of android, they are doing this through a small hack by collapsing their notification to top when the new notification arrives. – Chandru Feb 03 '16 at 19:52
0

It seem what you are looking for is a foreground service. Check http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

LazyClown
  • 752
  • 5
  • 11