I really need your help please. I'm trying to detect if there is internet connectivity on my Ionic 2 mobile app. I need to know at any time if the mobile lost connection and when it recovers it, also if there is wifi connection.. so the observators and the wifi detection of the Network Cordova plug-in, just does what I need For achieve that, I have two suscribers (for connection and disconnection) in the page constructor to set an boolean instance variable I used throught out the page logic. Here is my code
app.component.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { Geolocation } from '@ionic-native/geolocation';
import { Network } from '@ionic-native/network';
//import { Network } from 'ionic-native';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
Geolocation,
Network
]
})
export class AppModule {}
home.ts
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { Network } from '@ionic-native/network';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private online:boolean = false;
private wifi:boolean = false;
public disconnectSubscription:any;
public connectSubscription:any;
constructor(private nav: NavController, private geolocation: Geolocation, private network: Network, private platform: Platform) {
this.platform.ready().then( () => {
//listener
this.connectSubscription = this.network.onConnect().subscribe(() => {
alert('connected!');
this.online = true;
setTimeout(() => {
// before we determine the connection type. Might need to wait
if (this.network.type === 'wifi') {
this.hayWIFI;
alert('wifi connection');
}
}, 3000);
});
//listener
this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {
this.online = false;
alert('without connection!');
});
});
}
}
The problem is that the events are triggered like a onchange event.. I only see the alerts when I manually take out the connection of the mobile or when the app is recovering from sleeping (when it has lost the connection), but not when the app just initializes :( I have tried the constructor suscriptors' at the home page and even at the MyApp page (app.component.ts) without success
What am I doing wrong? How can I achive the requirements? There are many diferent approches on the internet but look older and differ even in the import of the Network Service. My ionic info is
Ionic Framework: 2.3.0
Ionic App Scripts: 1.1.4
Angular Core: 2.4.8
Angular Compiler CLI: 2.4.8
Node: 6.4.0
OS Platform: Windows 10
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
thanks in advance