The Notification Limit per app in Android

Dheeraj Andra
MindOrks
Published in
4 min readAug 15, 2019

Have you ever wondered if there is any notification limit per application in Android. I never thought there was one until I actually tried it out. Yes, there is a limit of notifications that can be posted per app. The interesting thing was the number is not fixed and can be controlled by the device Manufacturer. From my observation, a Google Pixel 3A phone had limited to 25 notifications where as a OnePlus 6T and a Samsung device had limited the notifications to 50.

Where has Android actually specified that there is a limit for notifications per app. Well, sadly after going through all the official documentation I couldn’t find anything wrt the limit of number of notifications that can be posted.

But there was a good stackOverflow post that I have come across which has mentioned that they have come up with an observation.

If we carefully observe the NotificationManagerService.java source code, we can see a variable MAX_PACKAGE_COUNT declared to be 50.

And inside the code, it checks if the count is greater than the MAX_PACKAGE_NOTIFICATIONS, it has a message saying that “Package has . already posted max toasts. Not showing more!”

This is the reason we get limited by the amount of notifications that are posted per app.

I request you to give it a try and check out the max number of notifications that can be posted by an app. You can use a simple for loop for this and at one point the notifications stop being posted.

Now, whats the solution for this? How come we can let the user know that the maximum number of notifications are reached. How can we continue posting notifications by applying a logic so that the user does not miss the recent notifications?

Well, let’s find that out in this article.

We will be creating a sample app. We will be scheduling notifications for every second. We will be keeping a track of also the active notifications in the Notifications bar.

So, let’s get started.

The project repo can be found on this link:

Let’s create a new project in Android Studio with a Blank Activity.

Now, in the on-create function, let’s create a Notification channel, since starting Android O we can’t display a Notification without a channel.

val notificationManager =
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//If the Sdk version is greater than 'O', configure a Notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)
// Configure the notification channel
with(notificationChannel) {
description = NOTIFICATION_CHANNEL_DESCRIPTION
enableLights(true)
vibrationPattern = longArrayOf(0, 1000, 500, 1000)
enableVibration(true)
notificationManager.createNotificationChannel(this)
}
}

Now, let’s create a function which checks for the active notifications and notifies the user:

/**
* Checks for the Maximum notifications and Notify the user
*
@param notificationManager The NotificationManager instance
*/
private fun checkAndScheduleNotifications(notificationManager: NotificationManager)

You can check in this function that we get the active notifications from notification manager or the NotificationListenerService.

What are activeNotifications?

Active Notifications are an array of StatusBarNotifications for the respective app which are present in the Status bar.Now we have two ways to get this:

NotificationManager.getActiveNotifications() (for API≥23)

NotificationListenerService.getNotifications() (for API(≤22))

Note:

Please ensure that the Notification access in the Settings is selected for the respective app(if NotificationListenerService is used). Otherwise accessing the StatusBarNotifications data is not possible

So what we are doing here is:

Firstly, we are sorting the activeNotifications based on the postTime :

Collections.sort(currentNotifications, compareNotificationByPostTime)

The compareNotificationByPostTime is our custom comparator that sorts the array by the notification post time

private val compareNotificationByPostTime = Comparator<StatusBarNotification> { o1, o2 ->
return@Comparator (o1.postTime).compareTo(o2.postTime)
}

Here is the logic where we check for maximum notification count and update the StatusBar notifications

when (currentNotifications.size) {
in NOTIFICATION_START_LIMIT..getMaxNotificationsCount() -> {
notifyUser(notificationManager)
}
else -> {
Collections.sort(currentNotifications, compareNotificationByPostTime)
if (currentNotifications.isNotEmpty()) {
notificationManager.cancel(currentNotifications.first().tag,
currentNotifications.first().id)
notifyUser(notificationManager)
}
}
}

We are checking if the activeNotifications are in the limit required. If not, we are cancelling the oldest posted notification form the status bar notification and then notifying the user with the newest one.

So, this way even the user does not clear the notifications for a certain period of time, he can still be updated with the latest notifications even if there is a limit of maximum notifications per app to be posted.

I hope this article has been of some use.

You can also check out my articles on Dagger for Android, Work Manager in Android

Thank you so much for your time.

Let’s connect on Twitter and LinkedIn

--

--