0

i'm creating android app, and using Firebase. I have 'Missions', and 'Mission' have status. If the status changes to 'Active' - I need to 'Wake up' my app and run the code of 'ActiveMissionActivity'. (The work of the service should be just 'Waking up the app' and go to another activity)

I could write missionsDbRed onChildChanged listener which will be activated when the status is active - but only when the app is running.

What is the good practice for this issue? My friend suggested me to use 'Android Service', but i'm not sure which type do I need, and if there is something that works well with the Firebase DB.

For example i've seen 'Firebase Cloud Functions' But i'm not sure which one is more suitable and why. Thanks

jonb
  • 845
  • 1
  • 13
  • 36
  • 1
    Rather than waking up the app (I guess what you mean by that is actually starting the app) at random times, which might be considered intrusive, why don't you try implementing a push-notification system that would trigger when the status changes to `active`? This way, the user has the choice to initialize the app or not by simply pressing on that notification. You can achieve this by using firebase functions and therefore by not using a service that would eventually drain up battery. – Catalin Ghita Feb 17 '18 at 09:22
  • I understand what you mean - But I need the client device to run code as soon as the status is active. It's critical that whenever the status is active, the clients device sends it's current gps location to the DB. So if it's up to the user to click on the app - it will be a problem for me. The user will get a notifications 5 minutes before it happens – jonb Feb 17 '18 at 09:29
  • 1
    In this case, you might want to try out a service that listens ,as you said, to a change in db. At this moment, I am not aware that you can achieve this task with firebase function, therefore have a look at this [link](https://stackoverflow.com/a/28535885/7201671). Regarding your title question, you do not need an Android service to notice changes in db actively(in foreground) but you might need one to check changes in background, as you specify later. – Catalin Ghita Feb 17 '18 at 09:40
  • I have 2 parts: 1. Mission changes to active and runs app code 2. The app code sends current location to the DB. I understand that part 2. will be executed with a service. And I know how to get the location - it's not the issue (But thanks). I'm asking about part 1 - What is the way - to get part 2. running ? What is the way to notice that the status is changed ? I could do it with Service as well - but i'm wondering wether there is a better way with Cloud Functions – jonb Feb 17 '18 at 09:48
  • 1
    Noticing a change in db through firebase functions is easy, running code on the device immediately after isn't that easy as it requires the app (process) to be alive. As pointed out [here](https://firebase.google.com/docs/functions/use-cases) you can pretty much do anything but not run code on your device if the app instance in cause is not active. If you can substitute the code that should be ran in the app to a function, you pretty much have a winner. Part1. when mission changes to active, run some code through firebase function that would have impact to db and then wake up app with service. – Catalin Ghita Feb 17 '18 at 10:07
  • Thanks, I understand the Cloud Functions aren't suitable for the need to run the code. I just didn't understand what I should do, and your last sentence: 'run some code through firebase function that would have impact to db and then wake up app with service' I can execute part 1 like this: Write a Service that listens to the DB, and being called when onChildChanged occured. Then i'll check if the status was changed to active, and if so run the part 2 (With the Location Service) In which part should I use Firebase Cloud Function? What did you mean by 'firebase fucntion'? Thanks – jonb Feb 17 '18 at 10:16
  • 1
    I meant Firebase Cloud Functions. I doubt you need 2 services in this case. As I said, create a function (there are many tutorials that explain the process of creating a firebase function) that checks some parameters' variation, at that point you can write to the database through the function (in cloud) and (let's say) change a marker in your database to 'true'. Then create a service that is triggered when that marker becomes true, initializes the app etc. Check [this link](https://firebase.google.com/docs/functions/database-events) for more info on functions related to db changes – Catalin Ghita Feb 17 '18 at 10:26
  • This part : "create a function ... database to 'true'" I don't need it. The status is being changed to 'Active' already, I don't need to create it. This part: "Then create a service that is triggered when that marker becomes true, initializes the app etc." is relevant. But you are basiclly saying my question. I asked how can I make a service that is triggered when the marker becomes true and initialize the app. Should be service be running the whole time? It would drain the battery, but maybe I need it to happen - i'm asking how to do it and what is the good practice. – jonb Feb 17 '18 at 10:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165323/discussion-between-johnsnowthedeveloper-and-catalin-ghita). – jonb Feb 17 '18 at 10:41

2 Answers2

2

As push-notifications are not a solution to your issue, the best way of handling this matter is by building a service that checks (in the database) if the value you are interested in has changed.

Now regarding the service, a bad idea would be to run a service that continuously checks that value. This would result in a huge battery drain and you risk your process to be killed as android is freeing up resources. you can notice this here.

You can fix this by telling the service when to start checking the value you are interested in.(e.g. by using Alarm API and assuming you know when this should happen) You can see something similar here.

Keeping your service alive as less as possible is probably the safest way.

As for google cloud functions, you might wanna use those for sending notifications prior to the actions that happen through the service you established.

Catalin Ghita
  • 794
  • 8
  • 26
0

So if the status changes to active while your app is closed, so that means the value changed somewhere out of your app right? If that's the case simply send a push notification to users device to wake it up. It is a lot more performant than using a background service.

Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • push notification is not sufficient, because as soon as the status is active - the client should sent it's location to the DB, therefore - a code must be running as soon as that event is triggered. – jonb Feb 17 '18 at 09:49
  • usually, push notification will get to users very fast, few seconds max and even some instant communication apps use push notification for receiving the first message, then do their own logic for the upcoming messages. – Alireza Ahmadi Feb 17 '18 at 09:58
  • BTW, Where the status gets changed? In your server? – Alireza Ahmadi Feb 17 '18 at 09:59
  • I'm using Firebase DB, I have 'Missions' child, every mission has a status. And the fact that push notifications are fast isn't helping me to the phone to change location to the DB. I need this: As soon as the status changes to active, the phone needs to send location to the DB. That's all. Push notifications will only tell the user that the status is changed. And if he won't be near his phone - The location won't be sent. – jonb Feb 17 '18 at 10:05
  • Well, you didn't answer my question, where this status data get changed? Its a location of the user at any given time? or its updated on your server based on some logic? Also, you can send data with push notification to the user device, do whatever you want without even showing a notification or opening the app, So this can be completely invisible to user and user doesn't have to be near his/her phone – Alireza Ahmadi Feb 17 '18 at 10:21
  • I don't understand your question - The status data is just 'Available' or 'Active' and being changed by another 'driver' on the mission at the db. Regardless, there is a field of 'User location' and 'Driver location' for each user and driver. Think of it like 'Get Taxi'. Once the driver 'Starts the mission', the status changes to active. And the user should send it's location for the DB so the driver can fetch it and see where is the user on the map. In addition, the driver sends it's location to the DB so the user can fetch it and to see it on the map. That's all the logic neccesarry. – jonb Feb 17 '18 at 10:27
  • So I suggest you find a way to find out about the changed value in your firebase consol (I don't know the details of using firebase), Then push the change to user device just once, Now your user know that he/she has to send the data to the server, Use a service to update the data as long as necessary. Then when you finished shut down the service. This way you only have about 1-2 seconds delay only for sending the first data and after that your android service can handle the job more efficiently while you're not running a service all day long and you're not draining users battery. – Alireza Ahmadi Feb 17 '18 at 10:37
  • "So I suggest you find a way to find out about the changed value in your firebase consol (I don't know the details of using firebase)" This is my question - I know how to get location, and send it via the service, so the next part is not relevant. I'm asking about the first part: The status changes to 'Active'. How can I notice it ? Should it be a service that is active permenently, or is it another way with cloud functions? Currently it seems that service that listens to the DB always is the way. – jonb Feb 17 '18 at 10:41
  • You should definitely be able to use some server-side code to find the changes. check this out https://firebase.google.com/docs/functions/use-cases – Alireza Ahmadi Feb 17 '18 at 10:46