Unable to get network connection after switching from wifi to cellular data


#1

Hi folks

We’re facing the problem that we (sometimes) lose internet connection after entering or leaving our wifi and changing from/to cellular data. After that we’re unable to retrieve any data from a web api.

The following code shows how we handle network changes:

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

export enum ConnectionStatus {
    Online,
    Offline
}

@Injectable()
export class NetworkHelper {

    private status: BehaviorSubject<ConnectionStatus> = new BehaviorSubject(ConnectionStatus.Offline);

    constructor(public network: Network, public eventCtrl: Events) { 
        this.initializeNetworkEvents();
    }

    public initializeNetworkEvents() {
        this.network.onDisconnect().subscribe(() => {
            if (this.status.getValue() === ConnectionStatus.Online) {
                console.log('WE ARE OFFLINE');
                this.eventCtrl.publish('network:offline');
            }
        });

        this.network.onConnect().subscribe(() => {
            if (this.status.getValue() === ConnectionStatus.Offline) {
                console.log('WE ARE ONLINE');
                this.eventCtrl.publish('network:online');
            }
        });
    }
}

The only way so far to recover is to either turn on and off flight mode or restart the app manually. This of course is not acceptable for the user.

Did anybody face the same issue and knows how to solve it?

Thanks and best regards
Timon