Using a listener after an android application is c

2019-01-15 22:17发布

I've just been reading about adding a service to my application, and since 7/8 there are now service restrictions to improve phone performance.

I've seen that the recommended approach is to use a job scheduler, but will that not just periodically start a new listener if I did that?

Basically I update my database, and using a snapshot listener I want to update my user in real time. When the app is closed, I'd like to send a notification.

My issues (if I'm correct) are that constantly making a new Firestore request will eat through my request allowance.

Also, if its a job scheduler it won't quite be real time?

I've read that you can use a foreground service, but this doesn't really seem like that task that needs a permanent notification and would annoy the user.

Anyone got any tips on how I'd implement this?

Thanks

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-15 23:03

Using a listener after an android application is closed

You can use a listener after an android application is closed, by not removing it. Once you are using a listener, you also need to remove it according to the life-cycle of your activity. But this will work only for a shot period of time because Android will stop your service if the app is not in the foreground. It does this to save resources when the app isn't being used. It also might stop your app from doing any networking, or even kill the app process completely. There's nothing you can do to prevent this, other than making it a foreground service, as you already mentioned.

A foreground service is probably not the best thing to do for your case, nor is it the best thing for your users. Read more about limitations on background services.

My recommendation is to use Firebase Cloud Messaging to notify your app when something has changed that it might be interested in. So your users will recieve notification even if they will keep their app closed.

查看更多
登录 后发表回答