1

As soon as the data enters the realtime database, I want to send a notification (with some data from the firebase, like title, description etc. that is posted to firebase realtime database) to the users nearby to the location(within the radius of 3 km) from where the data is posted to the firebase.

Till now I know how to get the data from nearby locations but I don't know how to send a notification (when the app is closed) as soon as new data enters the database from the user's nearby location..

If anyone know how to do that, please tell.

I have read documentations and saw demo apps but I can't figure out how to do that..

Here is how I am getting data posted in nearby locations of the user and displaying it in recycler view-

db = FirebaseDatabase.getInstance().getReference("REPORTED-CUBS")
    geofire = GeoFire(db!!.child("REPORTED-CUBS-LOCATION"))
val r_cubs_al = ArrayList<DataModel>()

geoQuery = geofire!!.queryAtLocation(GeoLocation(latitude, longitude), 3.0)

try {
    geoQuery!!.addGeoQueryEventListener(object : GeoQueryEventListener {
        override fun onKeyEntered(key: String, location: GeoLocation) {
            try {
                db!!.child(key)
                    .addValueEventListener(object : ValueEventListener {
                        override fun onDataChange(snapshot: DataSnapshot) {

                            val r_cubs: DataModel? =
                                snapshot.getValue(DataModel::class.java)

                            if (r_cubs != null) {
                                rv_police.visibility = View.VISIBLE
                                tvNoCUBSReported.visibility = View.GONE

                                r_cubs_al.add(r_cubs)

                                rv_police.adapter = ReportedCUBSAdapter(r_cubs_al)
                            }else{
                                rv_police.visibility = View.GONE
                                tvNoCUBSReported.visibility = View.VISIBLE
                            }
                        }

                        override fun onCancelled(firebaseError: DatabaseError) {
                            println("The read failed: " + firebaseError.message)
                        }
                    })
            }catch (e : Exception){
                e.printStackTrace()
            }
        }
Andy A.
  • 1,392
  • 5
  • 15
  • 28
  • Hi, I've been researching a bit about your issue and I have find some cases related to Firebase notifications when the app is closed, some of them says that depends on the mobile phone brand, and android version, or some modification or implementations on your code, for what I understand about your issue is that you are not able to send notifications through App1 to App2, when App2 is closed. – Vicky Jun 14 '21 at 10:21
  • Here are some of the links I was talking about, please let me know if are related to your issue: 1. this is a bit older but maybe it helps (https://github.com/firebase/quickstart-android/issues/41), 2. Push Notifications when app is closed - Stack Overflow related (https://stackoverflow.com/questions/24313539/push-notifications-when-app-is-closed) case and 3. Send a notification when the app is closed - other Stack Overflow (https://stackoverflow.com/questions/39674850/send-a-notification-when-the-app-is-closed) – Vicky Jun 14 '21 at 10:22

1 Answers1

0

To send the notification you can use Firebase Cloud Messaging (FCM). In this documentation there are the steps to set up FCM.

Also you might find useful this answer from another Stack Overflow post, that shows a code example.

Sergi
  • 135
  • 7
  • I think you didn't get my question. Actually there are two apps. And I want that whenever user post anything from 1st app so the users in the his nearby locations using the 2nd app will get a notification when the app is not opened. So, I think that the answer that you gave can't solve my problem... – Android coder Jun 01 '21 at 16:21
  • Okay, and do you have the 2 apps in a single project or you have a different project for each environment? – Sergi Jun 04 '21 at 10:10