1

I'm trying to detect internet connectivity on my react-native app.This works perfectly in ios but doesn't work on android.

First I tried importing net-info from react-native. Post this, I tried using the dependency react-native-community/net info "^4.2.2" but it doesn't work.I have tried everything possible like enabling jetifier and also configuring the android settings according to the androidX core but nothing seems to work.

 componentDidMount() {
    NetInfo.isConnected.addEventListener(
      "connectionChange",
      this.handleConnectivityChange
    );
  }

  componentWillUnmount() {
    NetInfo.isConnected.removeEventListener(
      "connectionChange",
      this.handleConnectivityChange
    );
  }

  handleConnectivityChange = isConnected => {
    console.log(" OfflineNotice handleConnectivityChange ")
    this.props.checkOnlineStatus(isConnected);
    this.setState({ isConnected });
  };

I expect the netinfo to work in android just like how it does in ios. Am I missing something to include in android. I have already included the required permissions in android. Please help me with this.I've been struggling since a long time.

Aishwarya R M
  • 123
  • 1
  • 10
  • Is there INTERNET and ACCESS_NETWORK_STATE permissions added in Android's Manifest file ? Please check it seperately for Android. If it's not there in AndoridManifest.xml file. Add and retry. – Jaimin Modi Sep 30 '19 at 07:30
  • Yes. I have added the internet permission in AndroidManifest.xml. It still doesn't work. – Aishwarya R M Sep 30 '19 at 07:31
  • Here is the complete demo. It might be useful. Please check it once, is there something missing at your side. Here is the link : https://reactnativecode.com/netinfo-example-to-detect-internet-connection/ – Jaimin Modi Sep 30 '19 at 07:33
  • NetworkInfo's isConnected being true does not necessarly mean you have internet access, only that you are connected to a Wifi router or mobile data. To check internet access is working, you need to make a get request to an url – matdev Sep 30 '19 at 07:44

0 Answers0