Cordova plugin network information not working

ionic plugin add cordova-plugin-network-information
 
npm install --save @ionic-native/network
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 { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Network } from '@ionic-native/network';
 
@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Network
  ]
})
export class AppModule {}
import { NavController, ToastController } from 'ionic-angular';
import { Network } from '@ionic-native/network';
 
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
 
  constructor(private toast: ToastController, private network: Network, public navCtrl: NavController) {
  }
}
ionViewDidEnter() {
  this.network.onConnect().subscribe(data => {
    console.log(data)
  }, error => console.error(error));
 
  this.network.onDisconnect().subscribe(data => {
    console.log(data)
  }, error => console.error(error));
}

I am getting nothing in return

SAme here i get nothing…

this.network.onConnect().subscribe(data => {

the console is not even triggered

Triggering occurs directly at the event of connecting to the network or disconnecting from it.

import { Injectable } from '@angular/core';
import { Network } from '@ionic-native/network';

@Injectable()
export class ConnectivityService {

	internetStatus: boolean = true;

	constructor(
		private network: Network,
	) {
		if (this.network.type === 'none') {
			this.internetStatus = false;
		}

		this.network.onDisconnect().subscribe(() => {
			this.internetStatus = false;
		});

		this.network.onConnect().subscribe(() => {
			this.internetStatus = true;
		});
	}

	isOnline(): boolean {
		return this.internetStatus;
	}
}

Hi Mates, I found the solution to this. It seems like network events only fire when the app is running on a device. I you would like to test with a browser, please run ionic cordova run browser :slight_smile: Happy coding from me in South Africa