Closed UesleyCarossi closed 4 years ago
Which platforms are you experiencing this on?
Which platforms are you experiencing this on?
Tested only on Android
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.
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 https://github.com/apache/cordova-plugin-network-information/pull/74 for https://github.com/apache/cordova-plugin-network-information/issues/64
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.
The problem is that the plugin behaves differently, based on the day it was cloned and executed npm i
, cordova platform add android@8.0.0
, 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.
I downgraded from version 2.0.2 to 2.0.1 and it works perfectly. 😉
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.
Maybe it's helpful, I use cordova-plugin-network-information in conjunction with cordova-plugin-background-mode.
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.
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.
Loosen the requirements of the plugin to it's barebone's essentials.
Requirements
Stop with next requirements
Kind regards Pieter
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.
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
Any update on this issue. I'm facing the same in 3.0.0 in ANDROID.
Bug Report
Problem
I need to run an event when connecting to wifi, but it ends up running at unexpected times.
What is expected to happen?
The expected is to occur only when connecting to the internet.
What does actually happen?
The onConnect subscription is firing when the application is opened, minimized> open, when connected to the internet.
Information
Open the application, minimize and open that the onConnect event will be triggered.
Command or Code
https://github.com/UesleyCarossi/test-network
Environment, Platform, Device
Version information
Plugins:
cordova-plugin-device 2.0.2 "Device" cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard" cordova-plugin-ionic-webview 4.2.1 "cordova-plugin-ionic-webview" cordova-plugin-network-information 2.0.2 "Network Information" cordova-plugin-splashscreen 5.0.2 "Splashscreen" cordova-plugin-statusbar 2.4.2 "StatusBar" cordova-plugin-whitelist 1.3.3 "Whitelist"
Checklist