Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-14132

[cordova-plugin-network-information] connection.type is stale on Android 7+ if network connectivity state changes while device is in Doze mode

    XMLWordPrintableJSON

Details

    Description

      The navigator.connection.type plugin property is set when a change in device network connectivity triggers a native callback.
      On Android, this native callback is via a BroadcastReceiver, triggered by the CONNECTIVITY_ACTION broadcast.
      This sends the result back to the JS layer which triggers the JS plugin handler to set the property.

      However, Android 7 introduces "background optimization" limitations and Android 8 introduces further broadcast limitations.
      The consequence is that the plugin's broadcast receiver is not guaranteed to be invoked if the network state changes while the device is in Doze mode.
      And therefore the navigator.connection.type property state can be stale with respect to the actual network connectivity state.

      This may be the root cause of CB-13585 and CB-12035.

      The solution to this issue on Android is probably to do the same as the iOS implementation:
      on resuming the app from the background, trigger an update of the navigator.connection.type property.
      On Android, this should mitigate the fact that the broadcast receiver may not have been called while the app was in the background.

      Note: my current workaround solution to this issue to is call the plugin's getInfo() function when my app resumes from the background.
      Although this function is not in the API documentation, calling it invokes a native method to retrieve the current device connectivity state asyncronously via a callback. I then use the callback result to manually update the navigator.connection.type property, making sure it's fresh when the app is resumed from the background.
      Since getInfo() is querying the native ConnectivityManager directly, it's guaranteed to get the correct network state from the device, as the following screenshot illustrates:

      Attachments

        1. connection_type.png
          13 kB
          Dave Alden

        Activity

          People

            Unassigned Unassigned
            dpa99c Dave Alden
            Votes:
            3 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated: