【发布时间】:2019-07-22 20:15:26
【问题描述】:
我使用network plugin,它在本机/Cordova 设备上运行良好。但它不适用于 PWA 应用程序(即当没有 wifi 时)。有什么理由吗?根据上面的文档,它也应该在browser 上工作。
注意:我已关注 this doc 创建 PWA 应用。
network-state.ts
import { Injectable } from '@angular/core';
import { Subscription } from 'rxjs';
import { Network } from '@ionic-native/network/ngx';
import { ShowToastService } from './show-toast.service';
@Injectable({
providedIn: 'root'
})
export class NetworkStateService {
private connectSubscription$: Subscription = null;
constructor(private network: Network,
private showToastService: ShowToastService) { }
WatchConnection() {
if (this.connectSubscription$) { this.connectSubscription$.unsubscribe(); }
this.connectSubscription$ = this.network.onDisconnect().subscribe(() => {
this.showToastService.showNetworkStateErrorToast('Your internet seems to be down! Please check your network settings!');
if (this.connectSubscription$) { this.connectSubscription$.unsubscribe(); }
this.connectSubscription$ = this.network.onConnect().subscribe(() => {
setTimeout(() => {
this.showToastService.toast.dismiss();
if (this.network.type === 'wifi' || this.network.type === '4g' || this.network.type === '3g' || this.network.type === '2g' || this.network.type === 'cellular' || this.network.type === 'none') {
this.showToastService.showNetworkStateSuccessToast('Internet connection available!');
this.WatchConnection();
}
}, 3000);
});
});
}
}
app.component.ts
async initializeApp() {
await this.platform.ready();
this.statusBar.styleDefault();
this.splashScreen.hide();
this.networkStateService.WatchConnection();// here
}
【问题讨论】:
标签: angular typescript cordova-plugins ionic4 progressive-web-apps