Permanently observe network changes in Ionic with firebase backend

Hey guys,

one question, that I’m thinking about for a long time now and didn’t find a good solution for, yet.

I’m using Ionic 2 and firebase as backend. I want to implement a function that sets a flag to true if the user is logged in to a certain wifi network (his home wifi). Otherwise the flag is to be set false. This flag needs to be stored at firebase database (not just local storage). It is important that the flag is permanently updated when the network connection changes and even if the app is running in background.

I thought about using the Native Network Plugin and it’s “onchange()” method. However, this may not be a proper approach.
For example: The user is logged in at his home wifi and the flag, hence, is set to ‘true’. Let’s assume, he disconnects from the wifi, but doesn’t have a mobile connection. Then the app knows, that he disconnected from the wifi, but the flag in the database cannot be updated as there is no connection to the internet.

So this will probably be the wrong approach and I would have to set up something like a ping that is sent from the server (Firebase) to check if the client is still active in the certain network??

What would you suggest here? Any idea and any help would be appreciated.

Important: The flag needs to be updated in all cases, even if the app is running in background!

Hmmm this seems a bit odd to be completely honest, like what if they just close the app completely so it’s not even running in the background? What if they turn their phone off? I don’t think there’s any reliable way of doing what you’re requesting, but maybe there’s another approach. Can you explain the purpose of this flag? :slight_smile:

Hi @mich356c and all others.

Sure, let me explain what this is about in a little more detail:
In my app, users can search for offers of other users. However, on the result page only those users are supposed to be shown who are currently at home.

Hence, I need to be able to differ between users who currently are at home and those who are not at home.
I could use the user’s geolocation for that. But I don’t want to permanently log the user’s location.
Or I could build a feature where the user toggles himself if he is currently at home or not. But then the user would always have to update this which is kind of annoying by time. How long would he really do that? Not very long, probably.

Thus, it would be better, if there was an automatic way to find out. And what is typical for being at home? Well, most people’s devices will be logged in to their home network. This is why I want to enable the user to define once which wifi is his home wifi and then monitor the “isAtHome”-status automatically by checking the wifi he is logged in to.

Regarding the cases you mentioned: If the phone is turned off, the flag is supposed to be “false”, as the user is not active in his home wifi. If the app is not running at all: Best would be to still get the information. But I could live with it, if in this case no isAtHome information is gathered :wink:

I hope it makes more sense for you now. Looking forward to your answer.

Alright, so 3 approaches:

  1. The user toggles their status themselves: By far the most simple one, and IMHO doesn’t sound too bad - depends how often they actually need to do it.

  2. GeoLocation: The user would need to mark their home once, and then update their status every X timespan. You shouldn’t save a new record every time, so continuously logging their location in this way shouldn’t be a problem.

  3. WiFi: Assuming you get the WiFi name through the Network plugin (I haven’t used it before, too lazy to look it up ;)), you would do the same as in 2. They say “My home WiFi is called this!” and you check on an interval, send the update, server saves the status with the time, voila!

In all of the approaches you should save when the status was updated, and depending on which approach you take, assume that the user is not at home when the status was last updated X hours/minutes/seconds ago. So if the phone goes off or they lose internet, you no longer receive the updates to their “last updated” time won’t change. If it’s too old, leave the result out.

Was written in a bit of a rush, hope it’s good enough!

@mich356c: It’s not only good enough, but very helpful, actually. Thank you, I appreciate your help! :slight_smile:

Getting the wifi-ssid already works using the WifiWizard plugin in combination with the native network-information plugin. Hence, users can already mark their home wifi and I can store it’s ssid in the database -> everything fine so far.

I like your idea of letting the app send an update to the database every x seconds/minutes. That could work… I learned that I can use setInterval() and others for this. I will give it a try.

Regarding the architecture: Keeping in mind, that the function with setInterval() should always be called, even when running in background, what would you suggest from where to call it?

Is there any possibility, to have it called even when the user didn’t open the app actively? Maybe you can trigger the initialization with a request from the server or anything similar?

setInterval would probably be the easiest way of doing it yea. You could make a service for it, and from the app component on platform.ready initiate the loop to send the update. Just Google Angular 2 services / Ionic 2 services and you’ll find something to help with that.

And no, that’s not possible. I think you’re worrying too much about the status being wrong because of something the user is or isn’t doing, but that’s just how it is. You can never completely prevent this, you will just have to assume that if the last update time is more than 2 * updateInterval seconds ago (for example), the user is inactive. Really the most consistent thing would be for them to set it themselves, as long as you don’t make it a hassle to change the status in the app (Like 1 or 2 taps max), I think they would actually do it, but you should still include the “if no update for X seconds = inactive” thing. :slight_smile:

It seems to work pretty well. Thanks for your help, @mich356c!

Just to update this after some more tests: It works well as long as the app is active. It does not work when the app runs in background. I will probably need a plugin for that. Does anybody have recommendations regarding a certain plugin?

Are you not already using this? :wink:

Not yet… :wink: I’m not sure about the battery consumption of this one, actually, since it prevents the app from going to sleep-mode. I need to take a closer look at it and possible other plugins…

Not only will that drain battery, but it will probably fail app store review. Unless you are only planning on distributing this privately you probably shouldn’t do that.

I believe a better solution would be to use gps, because you are allowed to check gps in the background. Just a thought :slight_smile:

Fair enough. However, this would mean my users need to have GPS tracking activated at all time. And most people I know don’t do that. Due to several reasons (battery usage, data protection, …). This is why I really want to use the WiFi method since this is always activated anyway.

There must be way to do this without failing the app store rules…

Some time passed and I tried many different things to realize my idea of using the wifi to check if a user is at his home-location or not. However, I didn’t manage to find a proper solution that really satisfied me.

Now I’m coming back here and giving the suggestion of @rloui a try (it took me some time to admit, that this might really be the better approach :wink: ).

However, I’m struggling here as well. I described this here and would really appreciate any help of you guys. Thank you very much :slight_smile: