cordova-plugin-network-information
cordova-plugin-network-information copied to clipboard
Android no internet triggering `online` function
Bug Report
Problem:
On Android, app starts with internet access, move phone into airplane mode and the online function triggers when the offline function should trigger. This is happening about 50% of the time. After taking phone off airplane mode, I can see online function trigger again and then on a 2nd time putting phone into airplane mode do I finally see it go offline
What is expected to happen?
Moving phone into airplane mode should trigger the offline function - every time.
What does actually happen?
On first time putting phone into airplane mode - I see online function trigger. After restoring phone and then putting back into airplane mode a 2nd time, the offline triggers as its supposed to. Its only happening on the first time.
Information
Included above.
Command or Code
Plugin version 2.0.2
Environment, Platform, Device
Android...its happening on Android 5 through 9. - all my test devices.
Version information
Plugin Version: 2.0.2
Ionic: Ionic CLI : 5.4.2 (C:\Users\RPO\AppData\Roaming\npm\node_modules\ionic) Ionic Framework : ionic1 1.0.0 @ionic/v1-toolkit : 1.0.22
Cordova: Cordova CLI : 9.0.0 ([email protected]) Cordova Platforms : android 8.1.0 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, (and 30 other plugins)
Utility: cordova-res : 0.6.0 native-run : 0.2.8
System: NodeJS : v10.16.3 (C:\Program Files\nodejs\node.exe) npm : 6.4.0 OS : Windows 7
Checklist
- [x ] I searched for existing GitHub issues
- [ ] I updated all Cordova tooling to most recent version
- [x ] I included all the necessary information above
Hi
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'); }
Can you check the navigator.connection.type when you receive the online event when switching to airplane mode? Or can you refactor to work with the type property instead of the events and share the results?
Kind regards
Hello I am facing that same issue. @PieterVanPoyer when diggin the issue, the subscription is not even call so your workaround doesn't apply if we want to whatch that event continiously...
BR
@PieterVanPoyer - I just saw your solution on how you are handling this. Are you certain this is working as you think it does?
https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
How are you handling other and unknown?
As well, .type is not supported on about half the browsers....it is on WebView for Android, but not Safari for iOS - doesn't Cordova use its own webview for iOS too? Its not listed in the supported section.
https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/type
@rolinger the implementation of cordova for Android and ios have nothing to do with the experimental browser implementation in the browsers.
Cordova does not use its own webview for ios. Cordova uses the WkWebview on ios (nowadays).
The implementation of ios and Android is handled on the native part of the app. The javascript part reacts on native events of the mobile app.
In my setup it used to be working good. We checked the navigator.connection.type before every api call.
FWIW, the most recent version of this plugin is 3.0.0.
Kind regards Pieter
@PieterVanPoyer - I am asking these detailed questions because I am considering moving to your method. Need to understand the pitfalls.
In your method, none is easy, but how are you handling other and unkonwn? I mean, what other types are there....or is it more of a known type not reporting its type properly? In what cases would other or unknown occur?
Yup...running 3.0.0 for sometime now.
Hey
This should be the possible constants. https://github.com/apache/cordova-plugin-network-information#constants
With their effective values : https://github.com/apache/cordova-plugin-network-information/blob/master/www/Connection.js
But, be aware, due to privacy concerns the types are not always fully provided.
So I just check is it none or not.
For that reason I do not check any other types.