apache / cordova-plugin-network-information

Apache Cordova Network Information Plugin
https://cordova.apache.org/
Apache License 2.0
464 stars 321 forks source link

Type none when change type Network un app background #82

Open anaaChacon opened 5 years ago

anaaChacon commented 5 years ago

Hello, I have a problem with Internet connections, I use the ionic Network plugin and the problem I have is, when the connection type changes from wifi to data and the application is in the background the type of connection it receives is NONE, I have to kill the application and start it again so that everything goes correctly, has anyone experienced this problem?

Siva021 commented 5 years ago

Yeah, I am also experiencing the same, i am getting network type none when app is idle for more than 20mins in the background. After reopening the app or changing to Airplane mode network working fine.. Seems like this issue is occurring to ionic latest

anaaChacon commented 5 years ago

the solution that it poses @PieterVanPoyer has solved the problem for me pm android, but in them there is iOS no solution, at the moment I am satisfied.

petver commented 5 years ago

I have the same problem (using ionic 4). Is there a solution for this?

bryplano commented 5 years ago

Looks like there is a PR to fix this on Android per annaChacon's comment (it just hasn't been merged): https://github.com/apache/cordova-plugin-network-information/pull/74

No idea about the status of the problem on iOS.

rricamar commented 5 years ago

Hi ✋ ,

The PR has been merged, you can now install the plugin with:

cordova plugin add https://github.com/apache/cordova-plugin-network-information#master --force

in order to have the last version, while the team make a release

PS: As long as I know, I couldn't reproduce this issue on iOS. Only on Android 🤔

halindrome commented 5 years ago

Was this merged into 2.0.2? And a related item - I don't seem to get events when the app is in the background. Is that addressed with this PR too?

janpio commented 5 years ago

Yes, #74 is part of 2.0.2: https://github.com/apache/cordova-plugin-network-information/blob/master/RELEASENOTES.md#202-jun-19-2019

PieterVanPoyer commented 5 years ago

Hello,

The #74 does not solve the network change trigger when the app is idle (or in the background) for android. So, it does not solve this issue.

The Pull Request solves that the networkinfo is not reliable when the app is in the foreground. Tries to solve issue-64 The plugin uses (before the PR and after the PR) the android BroadcastReceiver.

Android can stop the listeners on the BroadcastReceivers to save battery power when the app becomes idle. To solve this the android - plugin should be rewritten and use the JobScheduler API (min-SDK Lollipop). (see: https://developer.android.com/reference/android/app/job/JobScheduler.html and https://medium.com/@iiro.krankka/its-time-to-kiss-goodbye-to-your-implicit-broadcastreceivers-eefafd9f4f8a) But that should be another pull request.

Kind regards, PIV

PieterVanPoyer commented 5 years ago

@halindrome in our company we do not rely on the 'offline' and 'online' events. In our project, we allways check the navigator.connection.type before every api call.

const networkState = navigator.connection.type;
if (networkState === 'none') {
   console.log('We are offline');
} else {
   console.log('We are online');
}

As an addition on my previous comment, in my opinion the javascript part of an app can go idle too when android decides to save battery and when the app is in the background.

What is the use case for you to receive these events while the app is in the background? Can the 'type' - property check solve your problem? If not, provide a use case and maybe someone can start building a solution.

Kind regards, PIV

halindrome commented 5 years ago

Our application does a lot of things while the app is in the background. In general, on Android, these are successful. Even when the app is running javascript tasks those work in the background most of the time. I was relying upon this plugin (which has a non-JS component) to deliver events when the network changes state.

One of the things that is unique about our application is that it prefers to talk to a local gateway on a local network. A specialized appliance we supply to our customers. In many environments, the devices on which our application is installed do not even have access to the internet (and are not cellular or do not have their cellular data enabled). In these cases, especially when the WiFi support in the environment is uneven, we see a lot of network problems. Our application is designed to work around these dropouts by queueing messages and resending them when the network becomes available.

I will see what I can do with the navigator.connection.type setting. I suppose a combination of this and a careful check of the devices IP address would help the application understand if it had changed networks or something. This is a common event in our application, and when it happens we need to search for a gateway again since this may have changed (a separate gateway on each subnet, for example).

Anyway, thanks for the pointer!

[edit] I attempted to rely upon this property of the navigator object. Under cordova (under ionic 4) the state of the navigator.connection.type property is not updated when the app is in the background either. So the various services we have running continue to attempt to talk to the network even when it is disconnected. Now, I could look at the response of a network request and realize that we have lost connection and act upon that, but I am surprised that this property is not being updated. Is there something we need to do to ensure that it reflects the reality of the device's environment?

PieterVanPoyer commented 5 years ago

Just to know, did this work before the 2.0.2 release?

englund commented 5 years ago

We have the same issue when the data saver is on in Android. We have these problems both in version ^1.3.4 and 2.0.2.

PieterVanPoyer commented 5 years ago

Hey,

Do you mean you have a problem with saving data when the app is idle or in the background?

Is it possible for you to check version 2.0.1? Does the problem occur in that specific version.

Kind regards, PIV

englund commented 5 years ago

@PieterVanPoyer So data saver (Connections > Data usage > Data saver) is a setting in Android.

PieterVanPoyer commented 5 years ago

Hey Englund,

Thanks for the info. I wasn't aware of the android Data Saver functionality.

This thread is becoming a bit complicated and multithreaded. Maybe it is a good idea, to make a seperate issue for the Data Saver <-> Network information bug. With steps to reproduce and so.

Then we are able to fix halindromes problem here, and address the Data Saver bug in another issue.

Kind regards, Pieter

PieterVanPoyer commented 4 years ago

@anaaChacon Hey. Is the original issue still occuring? If not, can you close this issue? Kind regards.