Firebase在实时数据库发生更改时发送通知
在Firebase实时数据库(例如,在聊天应用程序中)进行一些更改之后,我发现了两种发送通知的可能方法:
I found two possible approaches to deliver notifications after some changes in the Firebase real time database (for example in a chat application):
-
可以按照我还找到了此处一种更简单的方法,仅使用 android服务来侦听数据库中的更改.
I have also found here another simpler approach using just an android service that listens for changes in the database.
在尝试实施其中一种方法之前,我想知道这两种方法的优点和缺点是什么,因为第二种方法似乎比简单得多第一个.
I would like to know what are the pros and the cons of the two approaches before trying to implement one of them and since the second one seems much simpler than the first one.
The android service
solution described in https://www.codementor.io/sundayakinsete/firebase-real-time-notifications-app-to-app-opkwbo6ba has serious limitations:
- 在应用程序未运行时不起作用(请记住,当用户将您的应用程序置于后台时,系统可能会决定终止该应用程序以释放设备内存)
- 它使用了额外的电池,RAM和网络数据,以保持服务主动侦听远程数据库更改
- 每个运行该应用程序的设备(即使在后台运行)也将计入100,000个同时连接到数据库的限制.
- 它不适用于iOS
另一方面,如果您使用 Firebase Cloud Messaging (通过Cloud Functions)或自定义服务器):
on the other side, if you use Firebase Cloud Messaging (via Cloud Functions or a custom server):
- 即使该应用已关闭,您也可以收到通知
- 您不会消耗额外的电池或CPU
- 对于在前台实际使用该应用程序的用户,您可以使用100,000个并发连接的数据库限制.
- 它可在iOS和Web上使用