0

As part of my learning of Google's Firebase and Android (Kotlin), I have come up with a simple app idea that I wanted to pursue and actually publish on Google store.

The idea is that you create a group, join people, and send short pre-defined messages as push notifications to everyone in the group. Kind-of like a pager or chat app.

  • Creating users and groups is done using Firebase Cloud Firestore --> that part works perfectly fine
  • For the notification part, within the group, I insert a notification object under the Group collection and have a database listener on my app side that creates the notification, simple implemention like this:
val docRef = DatabaseController.db.collection(NaggerGlobalNotifications.TABLE_NAME)
        docRef.addSnapshotListener { snapshot, e ->
            if (e != null) {
                Log.w(TAG, "Listen failed.", e)
                return@addSnapshotListener
            }

            for (dc in snapshot!!.documentChanges) {
                when (dc.type) {
                    DocumentChange.Type.ADDED -> {
                        val newNotification = dc.document.toObject<MyNotification>()
                        NotificationsController.createNotification(
                            this,
                            newNotification.title,
                            newNotification.message
                        )
                    }
                }
            }
        }

The "NotificationsController.createNotification" creates the notification using standard NotificationCompat.Builder which works perfectly fine.

Now comes the part with which I am struggling

My initial plan was to have a Service that has this listener running in the background. As soon as some notification is added to my Firebase Cloud Firestore node, the listener catches it and makes the notification on the group members' phones.

This works perfectly fine when the app is alive. When the app is killed, the notifications cease to work.

I tried a solution described here: https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android but I ended up here: Listen to firebase database changes when app is closed . In general, the proposed fix worked except...

It seems it is not possible to listen for Firebase database changes in the background

I could implement this is as a kind of a timer task, to call Firebase DB every 30 seconds or use the FCM messages instead of the database, but that begs the question:

  • In case of timer call - if I would have a million users and every 30 seconds there would be a million calls to firebase for updates, would I have to be a millionaire to buy that kind of bandwidth with Google Firebase?
  • In case of FCM - triggering FCM messages is possible only using Firebase console or with the need of a setting up a dedicated server (I think?) --> nice for big companies, but not for a single developer like me :(

In general, what I seek here is a bit of a guidance.

The app is kind of like a chat app with predefined short messages (like 100 characters max) and I guess I am trying to implement this with the least possible hardware expenses. Perhaps I'm being foolish here to believe this is possible without huge servers.

Thank you for any tips!

Keran

Keran
  • 171
  • 11
  • How about using a PendingIntent to set a timer within the phone? See https://stackoverflow.com/questions/4452565/start-app-at-a-specific-time. –  Apr 25 '20 at 15:11

1 Answers1

1

You can use Cloud Functions for Firebase

The developer writes code for a new function, selecting an event provider (such as Realtime Database), and defining the conditions under which the function should execute.

I have a chat app. I am using Functions and Realtime Database. When it is written a new message to database, I'm sending a notification to user. In this way, I do not need to take any action. Firebase makes the listening process for me and sends a notification.

Free mode:

Invocations => 125K/month

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
  • Interesting, I will take a look at this functionality. Thank you very much for the suggestion – Keran Apr 25 '20 at 15:33
  • I had a hard time understanding at first time but it works. Happy coding... – Kasım Özdemir Apr 25 '20 at 15:36
  • Finally managed to make it work :) I went through this tutorial first: https://firebase.google.com/docs/functions/get-started Then this was describing exactly what I wanted to achieve with sending notifications: https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#manage_topic_subscriptions_on_the_server The most difficult part for me was getting the Node.js stuff to work, it's really abstract for me... anyway thanks a lot! – Keran May 22 '20 at 19:49
  • Happy coding. I'm happy for you :) – Kasım Özdemir May 22 '20 at 20:34
  • So are you saying that send all messages as notifications to the user?. We can listen to database changes when the app is alive. The only case we need is when the app is killed handle it using notification? But how does server know whether the app is killed or not? – Manoj Perumarath Jun 24 '21 at 11:13
  • @ManojPerumarath The server doesn't know this. It always sends a message notification to the device. You will handle it, not the server, for example you won't show notifications when the app is in the foreground. – Kasım Özdemir Jun 24 '21 at 15:15
  • @KasımÖzdemir So if this is the case to follow. We don't need to listen to database changes at all. All we need is to handle FCM notifications. – Manoj Perumarath Jun 25 '21 at 07:50