Pager 应用程序 - 当应用程序被杀死时 - 收听 firebase 更改并发送通知

Pager app - When app is killed - Listen to firebase changes and send notification

作为我学习 Google 的 Firebase 和 Android (Kotlin) 的一部分,我想出了一个简单的应用程序创意,我想在 [=61] 上实现并实际发布=]商店。

这个想法是您创建一个群组,加入其他人,然后将简短的预定义消息作为推送通知发送给群组中的每个人。有点像寻呼机或聊天应用程序。

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
                        )
                    }
                }
            }
        }

"NotificationsController.createNotification" 使用标准 NotificationCompat.Builder 创建通知,效果非常好。

现在是我苦苦挣扎的部分

我最初的计划是在后台提供一个具有此侦听器 运行 的服务。一旦将某些通知添加到我的 Firebase Cloud Firestore 节点,侦听器就会捕获它并在组成员的手机上发出通知。

当应用程序处于活动状态时,这工作得很好。当应用程序被终止时,通知将停止工作。

我尝试了此处描述的解决方案:https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android but I ended up here: 。一般来说,建议的修复工作除了...

似乎无法在后台监听 Firebase 数据库的变化

我可以将其实现为一种计时器任务,每 30 秒调用一次 Firebase DB 或使用 FCM 消息而不是数据库,但这引出了一个问题:

总的来说,我在这里寻求的是一点指导

该应用程序有点像带有预定义短消息(例如最多 100 个字符)的聊天应用程序,我想我正在尝试以尽可能少的硬件费用来实现它。也许我在这里愚蠢地相信这在没有大型服务器的情况下是可能的。

感谢您的任何提示!

可然

您可以使用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.

我有一个聊天应用程序。我正在使用函数和实时数据库。当它向数据库写入新消息时,我会向用户发送通知。这样,我不需要采取任何行动。 Firebase 为我制作监听程序并发送通知。

自由模式:

Invocations => 125K/month