Hello all,
i’m creating an chat app in ionic 2.i want to detect wifi on/off and based upon that i have to call a method.please help me.
Note:
i have implented using cordova-network-information.but it works when page refreshes.
apart from this if there any solution please help me.
I am not sure how to detect if the connection is wifi or not but you can detect connection like this:
import { Network } from '@ionic-native/network';
declare var Connection;
constructor(public network: Network){
this.onDevice = this.platform.is('cordova');
this.network.onDisconnect().subscribe(() => {
//something
}
});
this.network.onConnect().subscribe(() => {
//something
}
});
isOnline(): boolean {
if (this.onDevice && this.network.type) {
return this.network.type !== Connection.NONE;
} else {
return navigator.onLine;
}
}
If you want to work with the hardware read this document.
It’s ok but it works when you constructor is called.my scenario is different.
i want to call method when user does wifi/internet on/off.
the best u can understand in whatsapp send message to a friend (off your wifi) friend will not receive you message.after sending just on you wifi the message will be send now.
but I just gave you solution for that, look at my methods,
this.network.onDisconnect() and onConnect()
Thanks a lot it’s works.
Here is code:
Network.onConnect().subscribe(() => {
//something
console.log(“Connected”);
});
BUT it calls two times console.log(); why?
Network.onConnect() is use to detect if you have internet connection or not, it not detect wifi on/off