不能为地理围栏触发多个通知

问题描述 投票:0回答:1

[如果我在地理围栏触发器上输入了多个便笺,并且在同一位置输入了多个便笺,并且仅显示1条通知,则我正在制作一个记事应用,当您输入地理围栏时会触发通知。

以下是代码:广播接收器

    class GeofenceBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
       //if (intent.action == ACTION_GEOFENCE_EVENT) {
            val geofencingEvent = GeofencingEvent.fromIntent(intent)

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

            if (geofencingEvent.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                Log.v(TAG, "context.getString(R.string.geofence_entered)")

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

                /*val foundIndex = GeofencingConstants.LANDMARK_DATA.indexOfFirst {
                    it.id == fenceId
                }*/

                // Unknown Geofences aren't helpful to us
                /*if ( -1 == foundIndex ) {
                    Log.e(TAG, "Unknown Geofence: Abort Mission")
                    return
                }*/

                for (geofence in fenceId){
                    sendGeofenceEnteredNotification(
                        context, geofence.requestId, intent.getStringExtra("note")!!
                    )
                }

            }
        }
   // }
}

--------------添加地理围栏的方法---------------------------

private fun createPendingIntent(context: Context, note: Note) : PendingIntent{
    val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
    intent.action = "MainActivity.treasureHunt.action.ACTION_GEOFENCE_EVENT"
    intent.putExtra("note", note.note)
    return PendingIntent.getBroadcast(context, note.date.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
}


fun addGeofenceForNote(note: Note, context: Context) {
    val geofencingClient = LocationServices.getGeofencingClient(context)

    val geofence = Geofence.Builder()
        .setRequestId(note.id)
        .setCircularRegion(note.latitude, note.longitude, GeofencingConstants.GEOFENCE_RADIUS_IN_METERS)
        .setExpirationDuration(Geofence.NEVER_EXPIRE)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
        .build()

    // Build the geofence request
    val geofencingRequest = GeofencingRequest.Builder()
        .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
        .addGeofence(geofence)
        .build()

    geofencingClient.addGeofences(geofencingRequest, createPendingIntent(context, note))?.run {
        addOnSuccessListener {
            Log.e("Add Geofence", geofence.requestId)
        }
        addOnFailureListener {
            if ((it.message != null)) {
                Log.w("TAG", it.message!!)
            }
        }
    }
}

我在同一位置制作了3个音符,第二个音符是触发广播接收器的音符。

P.S。即使地理围栏位于完全不同的位置,我仍然只能收到1条通知。据我所知,所有待处理的意图和通知都具有不同的ID。

提前输入

android kotlin notifications geofencing android-geofence
1个回答
0
投票

问题是电话由于某种原因无法创建所有通知。

修复它的代码是NotificationManager上的.apply{}

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()
    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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.