我制作了一个可以同时发送大量通知的应用程序,问题是如果通知数量大于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
我甚至尝试将它们分组到通知组中,但更多的通知也消失了。
谢谢