# Issue:

Observables not firing, 

$ ionic cordova plugin add cordova-plugin-network-information
$ npm install --save @ionic-native/network

### package.json
    "@ionic-native/network": "^4.12.2",
    "cordova-plugin-network-information": "^2.0.1",

### app.module.ts

`import { Network } from "@ionic-native/network";`

```
@NgModule({
  declarations: [ MyApp ],
  imports: [
    BrowserModule,
    ...
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [ IonicApp ],
  entryComponents: [ MyApp ],
  providers: [
    ...
    Network
  ]
})
```

### My code

```

ionViewDidLoad() {
    ...
    this.loadConnectionSubscriptions();
  }

async loadConnectionSubscriptions() {
    console.log(await new Promise(resolve => { setTimeout(() => { 
resolve(this.network.type); }, 3000); }));
    this.onConnectSubscription = this.network.onConnect().subscribe(
      () => {
        // Wait for connection info to be available
        setTimeout(() => {
          switch (this.network.type) {
            case "2g":
            case "3g":
            case "4g":
            case "cellular":
            case "ethernet":
            case "wifi":
              this.hasConnection = true;
              break;
            case "unknown":
            case "none":
              this.hasConnection = false;
              break;
            default:
              this.hasConnection = true;
              break;
          }
        }, this.WAIT_TIME);
      },
      error => {
        // Unable to fetch connection (?)
        this.alertCtrl
          .create({
            title: "Error fetching connection status",
            subTitle: `${error}`,
            buttons: ["Ok"]
          })
          .present();
      }
    );
    this.onDisconnectSubscription = this.network.onDisconnect().subscribe(
      () => {
        console.log(`${this.network.type}`);
        this.hasConnection = false;
      },
      error => {
        // Unable to fetch connection (?)
        this.alertCtrl
          .create({
            title: "Error fetching connection status",
            subTitle: `${error}`,
            buttons: ["Ok"]
          })
          .present();
      }
    );
  }
```
## Expected behavior:

- hasConnection variable must be set to true when connection is available
- The following line: `console.log(await new Promise(resolve => { setTimeout(() 
=> { resolve(this.network.type); }, 3000); }));` should yield the connection 
type, but it does yield null on both android and browser.
- onDisconnect should set hasConnection to false

## Current behavior:

- Both observables (onConnect, onDisconnect) never call their subscribers
- - The following line: `console.log(await new Promise(resolve => { 
setTimeout(() => { resolve(this.network.type); }, 3000); }));` should return 
"unknown" on browser or the current network type

#### Disclaimer

I do apologize if this is an error related to ionic's implementation of your 
plugin, but i think it's worth to check.

[ Full content available at: 
https://github.com/apache/cordova-plugin-network-information/issues/69 ]
This message was relayed via gitbox.apache.org for [email protected]

Reply via email to