Ionic 4 network.onConnect not working in background mode

553 Views Asked by At

In Ionic 4 while watching if the network connection is back or not using an ionic plugin for background mode, it is not working, it is activating the background mode but network subscribe is not working.

import { BackgroundMode } from '@ionic-native/background-mode/ngx';
import { Network } from '@ionic-native/network/ngx';
private backgroundMode: BackgroundMode,
private network: Network,

console.log('net connected at ' + moment().format('YYYY-MM-DD hh:mm:ss')) is called only after app comes in forground

this.backgroundMode.enable();
this.bgSubscription = this.backgroundMode.on('activate').subscribe(() => {
   console.log('active');  //going here
   this.bgNetworkSubscription = this.network.onConnect().subscribe(() => {
      // not going inside this while app is in background
      console.log('net connected at ' + moment().format('YYYY-MM-DD hh:mm:ss')); 

   });
});

I am using

@ionic-native/background-mode": "^5.23.0",
@ionic-native/network": "^5.23",

Ionic CLI         : 5.4.16
@angular/cli      : 8.3.25
Cordova CLI       : 9.0.0 ([email protected])

1

There are 1 best solutions below

0
On

Old question, but I stumbled upon this question when looking for an answer myself. Reading the documentation I realized that this is not possible with Network, the documentation states that:

This plugin is unable to broadcast events while in the background. Use navigator.connection.type to check connection status on the resume event instead.