Comments (10)
I've reproduced the described behaviour on an Android API 29 simulator using the latest cordova tooling and plugin.
Just posting some information... Couple of different theories.
Theory 1
Broadcast event is firing because we unregister and register to the native listener onPause/onResume. I assume the act of registering the event causes the event to fire at least once, producing an event to the javascript.
It didn't always unattach/re-attach the listener on pause/resume. This was changed by #74 for #64
Theory 2
The native broadcast simply fires when the activity is resumed.
I'm not really sure on the behaviour of the Intent broadcaster, but if theory 2 is actually what is happening, then I don't think this is a bug.
But if I were to make a bet, I think Theory 1 is why we are seeing these events on pause/resume. If we consider this a bug, then I think we should keep track of connection types, and only propagate the event to the javascript if the connection type actually changes. I'd support a PR that makes this change.
I'd also like to see if this behaviour is observable on iOS, but I don't have mac equipment to use to test this.
Hi
Thanks for the great work with Cordova!
I think Theory 1 is correct.
By reattatching the native listener it fires.
This reattaching helped to fix the bug where the connection state seems to be wrong after an amount of time when the app was idle.
I admit, it would be an improvement if we could keep the state of te connection and only propagate a change when the online/offline state effectivily changes.
In my opinion, this plugin is very usefull, but aims to do too much.
It was originally developed when there we're a lot less restrictions and rules in the Android system.
- Now the Android OS can stop the listeners for working when the app is idle.
- Now the Android OS is much more watching the battery drain, and stopping apps when they are idle.
- Now there are a lot of more restrictions to privacy, and this affects the detection of the effective Network.
- The ios system has even more restrictions about privacy.
In the documentation of this plugin there is an example of an upload of a file in the background https://github.com/apache/cordova-plugin-network-information#sample-upload-a-file-depending-on-your-network-state- , this used to work great, but now you have to work with WorkManager and stuff like that to do work in the background.
A suggestion
Loosen the requirements of the plugin to it's barebone's essentials.
Requirements
- Detect online/offline only when the app is active.
- When online, only detect following types: WIFI, MOBILE
- If possible, only fire when the state effectivily changes. So keep track of the previous state before the app went in the background. And the app is not killed by the system.
Stop with next requirements
- Detect online/offline when the app is not active.
- Don't try to detect other network type's beside WIFI and MOBILE.
- Do not let this plugin support background work like fileupload's and datasyncs. Remove it from the doc's.
A suggestion to do on short notice
- If #91 (comment) this comments work for the state. Maybe it should be mentioned in the README of this plugin.
Kind regards
Pieter
from cordova-plugin-network-information.
I downgraded from version 2.0.2 to 2.0.1 and it works perfectly. 😉
from cordova-plugin-network-information.
I've reproduced the described behaviour on an Android API 29 simulator using the latest cordova tooling and plugin.
Just posting some information... Couple of different theories.
Theory 1
Broadcast event is firing because we unregister and register to the native listener onPause/onResume. I assume the act of registering the event causes the event to fire at least once, producing an event to the javascript.
It didn't always unattach/re-attach the listener on pause/resume. This was changed by #74 for #64
Theory 2
The native broadcast simply fires when the activity is resumed.
I'm not really sure on the behaviour of the Intent broadcaster, but if theory 2 is actually what is happening, then I don't think this is a bug.
But if I were to make a bet, I think Theory 1 is why we are seeing these events on pause/resume. If we consider this a bug, then I think we should keep track of connection types, and only propagate the event to the javascript if the connection type actually changes. I'd support a PR that makes this change.
I'd also like to see if this behaviour is observable on iOS, but I don't have mac equipment to use to test this.
from cordova-plugin-network-information.
Which platforms are you experiencing this on?
from cordova-plugin-network-information.
Which platforms are you experiencing this on?
Tested only on Android
from cordova-plugin-network-information.
The problem is that the plugin behaves differently, based on the day it was cloned and executed npm i
, cordova platform add [email protected]
, cordova build android
.
A few weeks ago we were looking for some way to identify when the user connected to wifi, we tested and confirmed that the cordova-plugin-network-information plugin would perfectly fulfill this purpose, so analysis and implementations were made in the project, but when we went integrating with the plugin its behavior was different from what we had tested weeks ago, we have 2 apks to prove it.
from cordova-plugin-network-information.
I downgraded from version 2.0.2 to 2.0.1 and it works perfectly.
well that's definitely interesting. I don't have time to poke around right now, but having a list of change commits between 2.0.1 and 2.0.2 would be helpful. Sounds like there was a regression that was introduced.
from cordova-plugin-network-information.
Maybe it's helpful, I use cordova-plugin-network-information in conjunction with cordova-plugin-background-mode.
from cordova-plugin-network-information.
Hey
I did check the Android code and found something interesting.
On next lines, there is a check on the equality of the network state before and after a change.
The code seems to try to send an update only after a change. But this code goes long way back.
cordova-plugin-network-information/src/android/NetworkManager.java
Lines 221 to 233 in 9f85270
The thisInfo and lastInfo are JSONObjects.
But when I run following code in a testproject. 2 JSONObjects with the same values are not seen as equal.
try {
JSONObject connectionInfo = new JSONObject();
connectionInfo.put("type", "myType");
connectionInfo.put("extraInfo", "myExtraInfo");
JSONObject otherConnectionInfo = new JSONObject();
otherConnectionInfo.put("type", "myType");
otherConnectionInfo.put("extraInfo", "myExtraInfo");
prependToNetworkOutputState(this.tvNetworkStateOutputKeepsListening, "equals JSONObjects " + connectionInfo.equals(otherConnectionInfo)); // THIS EQUALS RETURNS FALSE
} catch (JSONException e) {
}
I found something like this on StackOverflow: the Android's built-in JSONObject does not define hashCode() and equals() in any sensible way.
Check also the Android Docs: Android Docs
Can anyone confirm this?
If yes, then we should just correct the equality check.
There is also another path we have to check for state, but that's for later.
Kind regards,
Pieter
from cordova-plugin-network-information.
Any update on this issue. I'm facing the same in 3.0.0 in ANDROID.
from cordova-plugin-network-information.
Related Issues (20)
- IOS 13.4 not working offline HOT 2
- core.js:6014 ERROR RangeError: Maximum call stack size exceeded HOT 2
- Refactor eslint to use cordova-eslint HOT 1
- Drop support for node < 10. HOT 1
- Version 2.0.2 from npmjs is crashing some devices with Android 10, on startup. HOT 9
- 5G detection HOT 13
- Correct network detection API calls deprecated on Android and iOS HOT 2
- network.connection.type is giving value as unknown in case of IOS 14 HOT 2
- Under no network conditions, start the App HOT 1
- What exactly is triggering online/offline events? HOT 2
- How to install specific version? HOT 2
- what 's new in version 3.0 ? should I need to update? HOT 1
- Issue with Ionic-native/network onConnect() on upgrade to Angular 10 HOT 1
- network.type is set to 'wifi' even when iPad Air is set to flight modus or when I am turning off WiFi
- Wrong typing for Connection
- 5G detect support HOT 4
- Network Up and Down Speed in android
- Navigator.connection support
- #throwInvalidProviderError HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cordova-plugin-network-information.