In React Native Android How to check internet connection is online /offline in application background?

19,962

Solution 1

import { NetInfo } from 'react-native';

NetInfo.isConnected.fetch().then(isConnected => {
           if(isConnected)
           {
               console.log('Internet is connected');
           }
})

This above code is sufficient to check if there is active net connection or not

Solution 2

NetInfo API is used for checking internet connection.

eg: NetInfo.isConnected.addEventListener('change', this._handleConnectionChange);

refer https://facebook.github.io/react-native/docs/netinfo.html for more details.

Share:
19,962

Related videos on Youtube

Saravana Kumar
Author by

Saravana Kumar

Updated on September 15, 2022

Comments

  • Saravana Kumar
    Saravana Kumar over 1 year

    I need to check Internet Connection while Application Background

  • Saravana Kumar
    Saravana Kumar about 7 years
    Is it NetInfo can check internet connection in application background also ? @JainZz
  • Saravana Kumar
    Saravana Kumar about 7 years
    I would check that code , but it can run only on application running time, that can't work in application background. @JainZz
  • Cobolt
    Cobolt about 7 years
    If you want it to work in the background you'll have to implement your code in java/objective-c natively
  • Rushi trivedi
    Rushi trivedi almost 6 years
    not working in iOS
  • Nabeel K
    Nabeel K over 5 years
    this gives 'connected' response if my internet is down but connected to wifi. How can I detect that case ?
  • hzak
    hzak over 4 years
    NetInfo is deprecated. This plugin needs to be used instead github.com/react-native-community/react-native-netinfo