ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
499 stars 574 forks source link

networkStatusChange event fires after on app resume #1014

Open selcuk-sahin opened 2 years ago

selcuk-sahin commented 2 years ago

Bug Report

Plugin(s)

@capacitor/network

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 3.5.1
  @capacitor/core: 3.5.1
  @capacitor/android: 3.5.1
  @capacitor/ios: 3.5.1

Installed Dependencies:

  @capacitor/cli: 3.5.1
  @capacitor/android: 3.5.1
  @capacitor/core: 3.5.1
  @capacitor/ios: 3.5.1

[success] iOS looking great! 👌
[success] Android looking great! 👌

Platform(s)

Android

Current Behavior

When the app resumes, the network state change Detector is activated. It also happens when using plugins that change app contexts, such as Facebook Login

Expected Behavior

This event should not be fired when app is resumed.

Code Reproduction

 Network.addListener('networkStatusChange', async (status) => {
      if (status.connected) {
        alert('Reconnected')
      } else if(!status.connected) {
        alert('Low connection')
      }
    });

Other Technical Details

Xiaomi Redmi 5 Plus Android v8.1.0

Additional Context

iamrsojitra commented 1 year ago

I am facing the same issue. Did you find any solution for this?

shprink commented 1 year ago

Same here with @capacitor/network@4.1.0

iamrsojitra commented 1 year ago

@shprink I found like why this is happening. It was happening because of power saving mode was on.

shprink commented 1 year ago

ok thanks but it is not the case with me.

image

webmasterab commented 1 year ago

When checking the connection I get a message 3x. As also indicated above.

@capacitor/network@4.1.0

If there is no connection or network, you will not receive feedback in the form of false and none. on Android12 Emulator Android Studio

selcuk-sahin commented 1 year ago

I'm using

@ionic/angular": "^6.4.1" @capacitor/network": "^4.1.0"

& here is a working solution for me:

...
import { ConnectionStatus, Network } from "@capacitor/network";
import { Platform } from "@ionic/angular";
import { PluginListenerHandle } from "@capacitor/core";
import { takeUntil } from "rxjs/operators";

@Injectable({
  providedIn: "root",
})
export class NetworkService implements OnDestroy {
  private ngUnsubscribe = new Subject();
  latestStatus: ConnectionStatus;
  networkListener: PluginListenerHandle;

  constructor(private platform: Platform, private commons: CommonsService) {}

  ngOnDestroy(): void {
    if (this.platform.is("capacitor")) {
      Network.removeAllListeners();
    }
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

  async init(): Promise<void> {
    await this.platform.ready();
    this.addNetworkListener();
    this.latestStatus = await Network.getStatus();

    this.platform.resume.pipe(takeUntil(this.ngUnsubscribe)).subscribe(() => {
      this.addNetworkListener();
    });

    this.platform.pause.pipe(takeUntil(this.ngUnsubscribe)).subscribe(() => {
      this.networkListener.remove();
    });
  }

  private async addNetworkListener() {
    this.networkListener = await Network.addListener("networkStatusChange", (status) => {
      /**
       * Detect if status change correct, i.e. dont trigger warning for same event twice
       */
      if (this.latestStatus.connected !== status.connected) {
        this.latestStatus = status;
        if (status.connected) {
          this.handleConnect();
        } else {
          this.handleDisconnect();
        }
      }
    });
  }
  ...
  }
webmasterab commented 1 year ago

I have deleted my previous answer.

Because it works anyway.

Thanks for this

webmasterab commented 1 year ago

Tested again today.

The network check works fine on Android 11 and below but not on Android 12 and above.

If you lose the connection in Android 12, the inspect indicates that the listener will be removed. When the connection is restored, the listener is activated again.

Very strange.

So it doesn't work well

-- When I test it with Android 11 in the emulator (latest version in Android Studio) both actions say that the event list is added.

With Android 12 in emulator in Android Studio, it says no network that the listener is being removed. That's where it goes wrong.

I've looked at network.java but can't figure it out.

webmasterab commented 1 year ago

Got it working by disabling the below

And adding this above this code

The code now looks like this for the part `` private async addNetworkListener() { this.networkListener = await Network.addListener("networkStatusChange", (status) => { /**

danis039 commented 3 months ago

It's 2024 now... 🙂 Any fix for this in the horizon?

marshall86 commented 2 months ago

any feedback about this bug? I'm using Angular 17, Capacitor 5 and Ionic 8. Every time the App resume, the networkStatusChange triggers in a loop