I am creating an android app which has a chat feature. I would like to create a notification if a new message arrives when the chat is not open. My messages are stored in a firebase database. I see two options for creating these notifications. One is to use the firebase function to trigger a firebase cloud message. The other is to use an Intent Service which runs an onChildAdded Event handler. The Intent Service seems much easier to me. Am I missing something? What would be a good reason to use Cloud Messaging over an Intent Service with the event handler running?
我正在创建一个具有聊天功能的Android应用程序。如果在聊天未打开时收到新消息,我想创建通知。我的消息存储在firebase数据库中。我看到了创建这些通知的两个选项。一种是使用firebase函数来触发firebase云消息。另一种是使用运行onChildAdded事件处理程序的Intent服务。意图服务对我来说似乎更容易。我错过了什么吗?在事件处理程序运行时使用Cloud Messaging而不是Intent Service的理由是什么?
1 个解决方案
#1
0
If you're worried your Service will keep running all the time (and draining your battery), then that's a good reason to use the cloud function. Moreover, there are chances are that your service might get killed.
如果您担心您的服务将一直运行(并耗尽电池电量),那么这是使用云功能的一个很好的理由。此外,您的服务可能会被杀死。
Because only a few processes are generally visible to the user, this means that the service should not be killed except in low memory conditions. However, since the user is not directly aware of a background service, in that state it is considered a valid candidate to kill, and you should be prepared for this to happen. In particular, long-running services will be increasingly likely to kill and are guaranteed to be killed (and restarted if appropriate) if they remain started long enough.
因为用户通常只能看到一些进程,这意味着除了内存不足之外,不应该杀死该服务。但是,由于用户没有直接了解后台服务,因此在该状态下它被认为是有效的候选者,您应该为此做好准备。特别是,长期运行的服务将越来越可能被杀死,并且如果它们保持足够长的时间,则可以保证被杀死(并且如果适当的话,重新启动)。
Finally, all the fuss you'd have to go through to deliver the results to an activity might be as painful as developing a cloud function.
最后,为了将结果传递给活动,您必须经历的所有大惊小怪可能与开发云功能一样痛苦。
In the company I work we decided to use the cloud function and it was pretty easy. We only needed to keep track of the FCM token of the devices and our function would monitor a certain node in our Real Time database. Every time somebody wrote there we'd get warned and would be able to act on it (grab the node, identify sender and receiver and with the saved FCM token send the notifications). We've used this tutorial to achieve what we wanted. Some links on how to write the cloud function, here, here and a So question that I also used here. The official docs too.
在我工作的公司,我们决定使用云功能,这非常简单。我们只需要跟踪设备的FCM令牌,我们的功能将监视我们的实时数据库中的某个节点。每当有人在那里写信时,我们都会收到警告并能够对其采取行动(抓住节点,识别发送者和接收者,并使用保存的FCM令牌发送通知)。我们已经使用本教程来实现我们想要的目标。关于如何编写云功能的一些链接,这里,这里和我也在这里使用的问题。官方文档也是如此。