I have solved the problem…it was becuz ionic-native version… i upgraded to 2.2.16 which is the latest…it started working fine
this my code now :
constructor(...........................) {
local.getLocalData().then((res) => { ///// getting API info that was called from a service and stored in storage
this.watchforDisconnect(); ///// call method for watching disconnection
this.watchforConnection(); ///// call method for watching for connection
this.jobs = res;
});
let disconnect = this.watchforDisconnect();
disconnect.unsubscribe();
let connected = this.watchforConnection();
connected.unsubscribe();
}
method: watchfordisconnect()
watchforDisconnect(){
let disconnectSubscription = Network.onDisconnect().subscribe(() => {
let toast = this.toastCtrl.create({
message: 'no connection available!',
duration: 2500,
position: 'top'
});
toast.present();
});
return disconnectSubscription; ///// returning value to be used for unsubscribe
}
method: watchforConnection:
watchforConnection(){
let connectSubscription = Network.onConnect().subscribe(() => {
let toast = this.toastCtrl.create({
message: 'we\'re back online',
duration: 2500,
position: 'top'
});
toast.present();
setTimeout(() => { //////// i wrote this just for testing
if (Network.type === 'wifi'){
this.connectionType = "we've got wifi";
} else if (Network.type === '2g'){
this.connectionType = "we've got 2g connection";
} else if (Network.type === '3g'){
this.connectionType = "we've got 3g connection";
} else if (Network.type === '4g'){
this.connectionType = "we've got 4g connection";
} else if (Network.type === 'cellular'){
this.connectionType = "we've got cellular connection";
} else if (Network.type === 'unknown'){
this.connectionType = "unknown network";
} else if (Network.type === 'ethernet'){
this.connectionType = "we've got ethernet connection";
} else if (Network.type === 'none'){
this.connectionType = "none";
} else {
this.connectionType = "type is empty bro";
}
}, 3000);
});
return connectSubscription;
}
The reason I posted this perhaps you guys have some suggestions on a better way to do this or just leave it like this , its clear that I am trying to keep watch for Internet availability. and even though this code is written in one of the components. it seems to trigger the toasts even when I’m navigating through different pages/components in the app.