首页 > 解决方案 > Firestore 查询最终会为相同的信息创建多个推送通知

问题描述

在我的 Kotlin 代码中,我正在查询 Firestore 的更改,并根据条件向用户发布通知。如果数据库中的 4 个字段正在更改,代码发送相同的通知 4 次是否正常?我期望只发送一个通知。我的代码如下:

docref.collection("Users").whereEqualTo("uid", userid!!)
        .addSnapshotListener { value, task ->

            if(task!=null){

                return@addSnapshotListener
            }

            for (document in value!!) {

                val current_user_bid_lead_num = document.getLong("bid_lead")!!

                val new_user_bid_lead_num = current_user_bid_lead_num + 1



                docref.collection("winning_bid").whereEqualTo("previous_seller_id",userid!!).addSnapshotListener{value, task->

                    if(task!=null){

                        return@addSnapshotListener
                    }

                    for (document in value!!){

                        Log.d("userid",userid)

                        val losing_seller = document.getString("previous_seller_id")
                        val deal_num_lost = document.getLong("deal_num")!!
                        val winningbid_bid_lead_num = document.getLong("bid_lead")!!

                        if(usersid == losing_seller && current_user_bid_lead_num < winningbid_bid_lead_num) {

                            LosingBidNotificationChannel()

                            val CHANNEL_ID = "Losing Bid"

                            val intent = Intent(this, Mydeals_seller::class.java).apply {
                                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                            }

                            val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

                            val builder = NotificationCompat.Builder(this, CHANNEL_ID)
                                .setSmallIcon(R.drawable.openbidicon)
                                .setContentTitle("OpenBid")
                                .setContentText("Your bid has lost the lead")
                                .setStyle(
                                    NotificationCompat.BigTextStyle()
                                        .bigText("Your bid is no longer leading for Deal #${deal_num_lost}")
                                )
                                .setPriority(NotificationCompat.PRIORITY_HIGH)
                                .setContentIntent(pendingIntent)
                                .setChannelId(CHANNEL_ID)
                                .setAutoCancel(true)
                                .build()


                            with(NotificationManagerCompat.from(this)) {

                                notify(id, builder)
                                id++
                            }
                        }

                        newdealnumref = FirebaseFirestore.getInstance().collection("Users")

                        newdealnumref.document(userid).update("bid_lead",winningbid_bid_lead_num).addOnSuccessListener {

                        }.addOnFailureListener{e->

                            Log.w("firestore_create_error","Error writing to document",e)
                        }
                    }
                }
            }
        }

所以我得到了正确通知弹出的结果,但我不期待通知,因为数据库上的多个字段已更改。

例如,val losing seller可能val winningbid_bid_lead_num会同时更改,导致发送 2 个通知。我该如何限制这个?

编辑:经过进一步测试,我注意到该应用程序似乎在累积通知,即使我清除它们并模拟该过程,一旦发布通知,弹出的通知总数将增加,例如从 10 到 16 .那么有没有办法清除之前清除的通知的内存。

更新- 问题似乎与以下部分有关:

with(NotificationManagerCompat.from(this)) {

                                notify(id, builder)
                                id++
                            }
                        }

但如果我不包括上述内容,我将无法显示超过 1 个新通知。以上是显示超过1个新通知但没有android累积计数器并根据上面的计数器多次发送相同通知的正确方法吗?例如,如果检查通知是否已清除,是否可以运行检查,如果已清除,则将计数器重置为 0?

标签: androidkotlingoogle-cloud-firestoreandroid-notifications

解决方案


不要使用我上面使用的方法,利用快照侦听器来检查更改。

考虑使用云功能。作为一个应用程序开发的新手,深入研究它们是令人生畏的,但是一旦你有了一个基础的云功能,就会完美地倾听特定领域的变化。


推荐阅读