Ricardo Rodrigues
Ricardo Rodrigues

Reputation: 406

Android not showing all notifications

I made an app that can send a lot notifications at the same time, the problem is if the number of notifications is bigger than like 6 or seven not all notifications show up, despite the log saying that all notifications were created and having unique ids.

here is the code of the service for the notifications

class GeofenceService : JobIntentService() {

    override fun onHandleWork(intent: Intent) {

        if (intent.action == GeofencingConstants.ACTION_GEOFENCE_EVENT) {
            val geofencingEvent = GeofencingEvent.fromIntent(intent)

            if (geofencingEvent.hasError()) {
                val errorMessage = errorMessage(this, geofencingEvent.errorCode)
                Log.e(TAG, errorMessage)
                return
            }

            if (geofencingEvent.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                Log.v(TAG, this.getString(com.superapps.ricardo.smithnotes.R.string.geofence_entered))

                val fenceId = when {
                    geofencingEvent.triggeringGeofences.isNotEmpty() ->
                        geofencingEvent.triggeringGeofences
                    else -> {
                        Log.e(TAG, "No Geofence Trigger Found! Abort mission!")
                        return
                    }
                }

                val notificationManager = ContextCompat.getSystemService(this, NotificationManager::class.java) as NotificationManager
                notificationManager.handleNotification(this, fenceId)
            }
        }
    }

    companion object {
        private const val TAG = "geofence-transitions"
        private const val JOB_ID = 573

        fun enqueueWork(context: Context, intent: Intent?) {
            enqueueWork(context, GeofenceService::class.java, JOB_ID, intent!!)
        }
    }

here the code for the notification generation

fun NotificationManager.handleNotification(context: Context, geofences: List<Geofence>){
    val list = ArrayList<Notification>()
    val set = mutableSetOf<Int>()
    val notificationIdBase = (Date().time / 1000L % Int.MAX_VALUE).toInt()
    this.apply {
        for (i in geofences.indices){
            val notificationId = notificationIdBase + i
            val notification = createGroupNotification(context, notificationId, geofences[i].requestId)
            list.add(notification)
            notify(notificationId, notification)
            set.add(notificationId)
            Log.d("TAG", notificationId.toString())
        }

        Log.d("TAG", geofences.size.toString() + " " + list.size + " " + set.size)
        for (id in set){
            Log.d("TAG", id.toString())
        }
    }
}

and here the logs for a case with 29 notifications generated but only 24 showed up on the phone.

2020-05-04 16:46:32.944 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607192
2020-05-04 16:46:32.948 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607193
2020-05-04 16:46:32.952 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607194
2020-05-04 16:46:32.956 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607195
2020-05-04 16:46:32.958 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607196
2020-05-04 16:46:32.960 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607197
2020-05-04 16:46:32.965 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607198
2020-05-04 16:46:32.968 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607199
2020-05-04 16:46:32.971 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607200
2020-05-04 16:46:32.974 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607201
2020-05-04 16:46:32.977 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607202
2020-05-04 16:46:32.979 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607203
2020-05-04 16:46:32.981 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607204
2020-05-04 16:46:32.983 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607205
2020-05-04 16:46:32.985 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607206
2020-05-04 16:46:32.987 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607207
2020-05-04 16:46:32.990 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607208
2020-05-04 16:46:32.992 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607209
2020-05-04 16:46:32.994 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607210
2020-05-04 16:46:32.996 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607211
2020-05-04 16:46:32.998 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607212
2020-05-04 16:46:33.000 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607213
2020-05-04 16:46:33.002 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607214
2020-05-04 16:46:33.004 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607215
2020-05-04 16:46:33.005 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607216
2020-05-04 16:46:33.008 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607217
2020-05-04 16:46:33.009 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607218
2020-05-04 16:46:33.010 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607219
2020-05-04 16:46:33.012 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 1588607220
2020-05-04 16:46:33.012 31374-31459/com.superapps.ricardo.smithnotes D/TAG: 29 29 29

I even tried grouping them in a notification group but even more notifications disappear.

Thanks

Upvotes: 3

Views: 299

Answers (3)

mohosyny
mohosyny

Reputation: 1042

use this code Support all Versions

     lateinit var notification: NotificationCompat.Builder
        lateinit var manager: NotificationManager
        val channel = "NotificationTest"
        lateinit var notificationChannel: NotificationChannel


         manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationChannel = NotificationChannel(channel, 
             "notification",NotificationManager.IMPORTANCE_DEFAULT)
                manager.createNotificationChannel(notificationChannel)
            }

            notification=NotificationCompat.Builder(this,channel)
                    .setContentTitle("Title")
                    .setContentText("this is for test")
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)


    manager.notify(1,notification.build())

Upvotes: 0

Mayank Gupta
Mayank Gupta

Reputation: 59

If you are working on the android versions above oreo, then you have to use a notification channel for notifications from the notificationManager. I think it will work, as it did in mine.

Upvotes: 0

Lokendra Choudhary
Lokendra Choudhary

Reputation: 479

As mentioned by Tenfoure04 in comments, there's a hard limit on notificationns per package(https://github.com/aosp-mirror/platform_frameworks_base/blob/439ac26e6792b38deff4166dc7c1b35ddce18804/services/core/java/com/android/server/notification/NotificationManagerService.java#L291).

Another problem that you might be facing is bundling of notifications by android. Similar notifications might be getting bundled. Just try to decrease the frequency of notifications.

Upvotes: 1

Related Questions