Skip to content

Instantly share code, notes, and snippets.

@Ayush783
Last active February 10, 2024 13:18
Show Gist options
  • Save Ayush783/bc1f2c26489085685a227c77169deb69 to your computer and use it in GitHub Desktop.
Save Ayush783/bc1f2c26489085685a227c77169deb69 to your computer and use it in GitHub Desktop.
Sorting active notifications on android
// 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