You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would expect this plugin always to reflect the true network status of a mobile device, being notified when the status changes with its proper value
What does actually happen?
In general it works fine, but at some point I get stuck with a notification indicating that there's no network connection while the mobile device actual does have a connection. It's hard to reproduce, most time it works fine but on some occasions it doesn't. I subscribe to both onConnect and onDisconnect events.
This provides a really bad user experience since there are many web services that are not being called depending on the network status (some others are cached locally since I try to provide offline support and then sync to the server when connection is back online), like the login web service.
I'm not sure if I should rely entirely on this plugin anymore or if I'm doing something wrong. Does anyone had this behavior? I've seen it mostly on Android devices.
Thanks in advance!
Information
I subscribe to the provided events by the plugin on a service I use across the application, and when they are fired I check the network type just in case.... but to double check I set a timeout of 2 seconds and then re-check the current network type. Only then I change the status based on the type. If by some reason a subsequent event is fired I clear the timeout to prevent this with messing with the current network status from the last fired event
Command or Code
public online: boolean;
private notify;
constructor(private _pf: Platform, private _nw: Network) {
this.initNetwork();
}
public initNetwork(){
this._pf.ready().then(() => {
if (this._pf.is('cordova')) {
this._nw.onDisconnect().subscribe(() => {
console.log('now type... ', this._nw.type);
this.clearNetworkNotification();
this.notify = setTimeout(() => {
console.log('just to be sure.. ', this._nw.type);
if (this._nw.type.toLowerCase() === 'none') {
console.log('we are offline!');
this.online = false;
}
}, 2000);
});
this._nw.onConnect().subscribe(() => {
console.log('now type... ', this._nw.type);
this.clearNetworkNotification();
this.notify = setTimeout(() => {
console.log('just to be sure.. ', this._nw.type);
if (this._nw.type.toLowerCase() !== 'none') {
console.log('we are online!');
this.online = true;
}
}, 2000);
});
}
});
}
private clearNetworkNotification() {
if (this.notify) {
clearTimeout(this.notify);
this.notify = undefined;
}
}
public getNetworkStatus(){
return this.online;
}
Environment, Platform, Device
I'm building an Ionic 4 application in Angular 7 with cordova
You seem to be using version 2.0.1 of this plugin cordova-plugin-network-information 2.0.1 "Network Information"
First try to update to version 2.0.2. That is the current version on npm.
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 any of these remarks help in solving your problem?
Bug Report
Problem
What is expected to happen?
I would expect this plugin always to reflect the true network status of a mobile device, being notified when the status changes with its proper value
What does actually happen?
In general it works fine, but at some point I get stuck with a notification indicating that there's no network connection while the mobile device actual does have a connection. It's hard to reproduce, most time it works fine but on some occasions it doesn't. I subscribe to both onConnect and onDisconnect events.
This provides a really bad user experience since there are many web services that are not being called depending on the network status (some others are cached locally since I try to provide offline support and then sync to the server when connection is back online), like the login web service.
I'm not sure if I should rely entirely on this plugin anymore or if I'm doing something wrong. Does anyone had this behavior? I've seen it mostly on Android devices.
Thanks in advance!
Information
I subscribe to the provided events by the plugin on a service I use across the application, and when they are fired I check the network type just in case.... but to double check I set a timeout of 2 seconds and then re-check the current network type. Only then I change the status based on the type. If by some reason a subsequent event is fired I clear the timeout to prevent this with messing with the current network status from the last fired event
Command or Code
Environment, Platform, Device
I'm building an Ionic 4 application in Angular 7 with cordova
Version information
Checklist
The text was updated successfully, but these errors were encountered: