Reputation: 154
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])
Upvotes: 1
Views: 562
Reputation: 73367
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.
Upvotes: 0