16

I tried some suggestions, such as navigator.onLine, but even in flight mode, my app "thinks" its online.

I found some suggestions with ajax too, but I just want to check if I'm online to open an external web page. If not, I intend to show a message such as "Your device seems to be offline. Check your connection!".

Geziel Carvalho
  • 191
  • 1
  • 3
  • 12
  • and what seems to be the problem? can't you do if you get an error when trying to connect to service show that message? – MatejMecka Aug 02 '16 at 12:33

5 Answers5

17

The best approach is to use cordova network information plugin which does the job for you seamlessly. This plugin provides information about the device's cellular and wifi connection, and whether the device has an internet connection.

You check out the official github page of this plugin for more info on the same. Hope it helps.

Gandhi
  • 11,875
  • 4
  • 39
  • 63
10

Download the plugin: https://www.npmjs.com/package/cordova-plugin-network-information

And Try This.

document.addEventListener("deviceready", function(e){
        console.log(navigator.connection.type);
        document.addEventListener("offline", function(e){
                            alert("NO_NETWORK");

        }, false);  
}, false);  

offline

The event fires when an application goes offline, and the device is not connected to the Internet.

document.addEventListener("offline", yourCallbackFunction, false);
Tuhin
  • 3,335
  • 2
  • 16
  • 27
7

you can use this plugin

then you can use it everywhere in your App without to import it

if(navigator.connection.type === 'none') {
   alert('there is no internet')
 }

and you can add it into setInterval to check for internet connection every 5 seconds

  setInterval(() => {
        if(navigator.connection.type === 'none') {
            alert('there is no internet')
        } }, 5000);

there are another values for "avigator.connection.type" for example when there is internet then the value of "avigator.connection.type" is the type of connection (wifi, 4g, 3g, ethernet on windows .... )

Ali Mohammad
  • 794
  • 9
  • 16
4

By this

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);
}

checkConnection();
Naitik
  • 1,455
  • 15
  • 26
  • 1
    when the device connected via WIFI, this function will give us a true even if there is no internet connection in the hotspot – Hamza Kouadri Jul 24 '18 at 21:15
0

you can check the connectivity of your android device to internet by using cordova-plugin-network-information plugin. furthermore if you want to perform certain task on the availability of the internet, see below:

if (navigator.connection.type == "none") {
    alert("No Internet Connection...");
    
}
else {
    yourdesiredFunction();
}
Haseeb Javed
  • 63
  • 2
  • 13