Last active
February 10, 2024 13:18
-
-
Save Ayush783/bc1f2c26489085685a227c77169deb69 to your computer and use it in GitHub Desktop.
Sorting active notifications on android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// METHOD TO SORT | |
sort() { | |
activeNotifications = notificationManager.getActiveNotifications(); | |
Arrays.sort(activeNotifications, PushNotificationUtil.postTimeComparator); | |
while (activeNotifications.size() > 6) { // You can change this to any number. This defines the limit on notifications | |
StatusBarNotification notification = activeNotifications.remove(0); | |
notificationManager.cancel(notification.getId()); | |
} | |
} | |
// COMPARATOR FOR SORTING NOTIFICATION LIST | |
public static Comparator < StatusBarNotification > postTimeComparator = new Comparator < StatusBarNotification > () { | |
@Override | |
public int compare(StatusBarNotification sbn1, StatusBarNotification sbn2) { | |
// compare the post times | |
return Long.compare(sbn1.getPostTime(), sbn2.getPostTime()); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment