且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中创建Firebase后台服务?

更新时间:2022-12-27 12:29:13

我想在android中创建后台服务,以侦听Firestore中创建的新文档.

I'd like to create background service in android that listen for new Document created in Firestore.

您确实可以在Android服务中执行此操作,但是请记住,服务仅代表您可以告诉OS您必须完成一些后台工作且实际上并不需要附加视图(活动).

It's true that you can do that in an Android Service but please rememeber that a service represents only a way in which you can tell the OS that you have some background work that has to be done and that doesn't really requiere an attached view (activity).

根据有关 Android服务的官方文档,如果您想从中受益尽可能运行的服务:

According to the official documentatation regarding Android services, if you want to benefit from a running service as much as possible:

前台服务执行一些用户注意到的操作.例如,音频应用程序将使用前台服务来播放音轨.前台服务必须显示通知.即使用户未与应用程序进行交互,前景服务也将继续运行.

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.

换句话说,您需要为通知提供一个图标,以便通知用户该应用正在消耗资源.

So with other words, you'll need to provide an icon for the notification, so the user is informed that the app is consuming resources.

根据您的评论:

不是,我想听在Firestore中创建的新文档.

Nope I want to listen for new documents created in Firestore.

是的,您可以在数据库中甚至某个查询中附加一个侦听器,并且该侦听器会随着结果的更改而更新.但是请记住,您将必须对所获得的每个更新都进行读操作,这基本上意味着用户还将承担带宽成本以及电池耗电成本.因此,我总是建议根据生活情况移除听众活动的骑自行车者.

Yes, you can have a listener attached to some document in your database or even to a query, and it will update as the results change. But remeber that you will have to pay a read operation for each of the updates that you get, basically meaning that the user will also have the cost for the bandwidth, and the cost of their battery drain. So I always recommend remove the listeners according to the life-cycler of the activity.

当您想在后台听一些文档时,这是您与Firestore进行交易的一种方式,但也请考虑使用 Firebase的云功能:

That's a way in which you you deal with Firestore when you want to listen to some documents in a background but please also consider using Cloud Functions for Firebase:

用于Firebase的云功能使您可以自动运行后端代码,以响应由Firebase功能和HTTPS请求触发的事件.您的代码存储在Google的云中,并在托管环境中运行.无需管理和扩展自己的服务器.

Cloud Functions for Firebase let you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.

云功能实际上如何工作?

How do Cloud Functions actually work?

与客户端一样,Cloud Functions还允许您将侦听器附加到单个文档甚至查询.因此,当数据库中发生某些特殊情况时(例如,将某些文档写入Firestore集合中时),可以触发Cloud Function.触发功能后,您可以采取一些措施.正如弗兰克·范·普菲伦(Frank van Puffelen)在其评论中提到的,您可以发送例如通知.对于Android,请参见下面的简单示例:

As in the case of the client, Cloud Functions also allows you to attach a listener to a single document or even to a query. So a Cloud Function can be triggered when something special in your database happens, for instance when some documents gets written into a Firestore collection. Once the function is triggered, you can take some action. As Frank van Puffelen mentioned in his comment, you can send a notification for example. For Android, please see below a straightforward example:

发送通知可能不是您想要的,但这只是一个简单的示例,我认为您将能够理解.

Maybe this is not what you want, to send a notification but it's a simple example and I think you'll be able to get the idea.