OnDisconnect() firing twice alert


#1

Hi All… Have you tried Network status
steps which I followed using Ionic 2 to know whether you are online or offline

  1. install

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

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


constructor( private network: Network ){

       let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
               this.events.publish('network:online', false);
               this.online = false;
         
           console.log('network was disconnected :-(');

           let alert = this.alertCtrl.create({
               title: 'Network was disconnected :-(',
               subTitle: 'Please check your connection. And try again',
               buttons: ['OK']
           });
           alert.present();
       });
}

It firing twice Is anyone have solution for this and onConnect() is firing one time only
and I have checked this website

Ionic network doc
ampersandacademy
stackoverflow
Ionic forum
github

Please give me a solution if u solved means and I have ref websites please don’t suggest which I have already mentioned.

Thanks in advance


#2

Why are you not making a custom call for checking if the network is online?
Just simply create a provider instance for it.
In this provider you got the following sample code:

// -------------------------------------------------------------------------------------------------------------------------
    // @network
    // -------------------------------------------------------------------------------------------------------------------------
    // Check if the network is online/offline
    // -------------------------------------------------------------------------------------------------------------------------
    networkStatus()
    {
        var oHeaders = new Headers();
        oHeaders.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

        this.http.post('http://URL/network-status', {headers: oHeaders})
        .subscribe(oRes => {

            this.bOnline = true;

        }, (sError) => {

            this.bOnline = false;

        });
    }

Simply then return in your PHP function a true.
Then you can check back in your provider if the network is online with a var named like bOnline (true/false) (b stands for Boolean).

Hope I helped.