安卓手机无法显示所有通知

3

我制作了一个可以同时发送大量通知的应用程序,问题是如果通知数量大于6或7个,不是所有通知都会显示出来,尽管日志显示所有通知都已创建并具有唯一的ID。

以下是通知服务的代码:

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!!)
        }
    }

这是生成通知的代码

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())
        }
    }
}

这是一个有29个通知的案例的日志,但只有24个通知显示在手机上。

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
我甚至尝试将它们分组到通知组中,但更多的通知也消失了。
谢谢

每个包的通知数量有一个硬性限制,最多为25个:https://github.com/aosp-mirror/platform_frameworks_base/blob/439ac26e6792b38deff4166dc7c1b35ddce18804/services/core/java/com/android/server/notification/NotificationManagerService.java#L291 当通知数量超过一定数量而没有被清除时,您可以考虑手动折叠多个通知中的信息。取消所有现有通知并发送一个新的通知,其中包含用户的组合信息。 - Tenfour04
问题出现在25之前,比如我输入12它显示11,如果我输入13它显示12,24则显示21。 - Ricardo Rodrigues
这是针对一个设备还是所有设备都出现了这个问题?(如果这是一个特定于设备的限制,最好也在其他设备上进行检查) - auspicious99
我在两个真实的小米设备和模拟器上进行了测试。 - Ricardo Rodrigues
3个回答

1

问题发生在限制之前。如果我有10个通知,只有9个会出现。第一个不会出现。 - Ricardo Rodrigues

0

如果您正在使用Oreo以上的Android版本,则必须使用通知渠道来管理NotificationManager中的通知。我认为这将有效,因为在我的设备上已经实现了。


我不可能有一个频道。 - Ricardo Rodrigues

0
请使用以下代码:支持所有版本
     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())

我不可能没有频道。 - Ricardo Rodrigues

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接