What I did here is I added a check if we store more than 20 message to the addNotification() method. If we do, we call the cleanUpNotifications() method asynchronously if it is not running already. That way we can get rid of that aweful loop while still preventing too much memory being occupied by messages. After all, anything up to 20 messages should be perfectly fine.
I just tested it on my device and it works fine.
What I did here is I added a check if we store more than 20 message to the addNotification() method. If we do, we call the cleanUpNotifica tions() method asynchronously if it is not running already. That way we can get rid of that aweful loop while still preventing too much memory being occupied by messages. After all, anything up to 20 messages should be perfectly fine.
Note that I also added a test for that.