Http 请求在后台模式下不工作
Http request not working on background mode
我为 Reddit 写了一个 http 请求,但没有在应用程序组件上工作
我在其他页面上复制了代码并且工作正常但是在应用程序组件上无法正常工作 Http 请求(和我的私人服务)
import { Http, Response,Request } from '@angular/http';
import 'rxjs';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
constructor(public http: Http,public agornaApi:AgornaApi ,private backgroundMode: BackgroundMode ,public localNotifications: LocalNotifications,platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public storage: Storage,public authService: Auth) {
localNotifications.on("click", (notification, state) => {
this.nav.push(ListmessagesPage);
});
document.addEventListener('deviceready', function () {
// Android customization
// To indicate that the app is executing tasks in background and being paused would disrupt the user.
// The plug-in has to create a notification while in background - like a download progress bar.
cordova.plugins.backgroundMode.setDefaults({
title: 'بارگزاری',
text: ''
});
// Enable background mode
cordova.plugins.backgroundMode.enable();
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {
setInterval(function () {
this.http.get('https://www.reddit.com/r/gifs/new/.json?limit=10').map(res => res.json()).subscribe(data => {
cordova.plugins.notification.local.schedule({
text: data.kind,
data: { secret:'key' }
});
});
}, 10000);
}
}, false);
});
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
setTimeout(() => {
splashScreen.hide();
}, 100);
});
}
```
系统信息
全局包:
@ionic/cli-utils : 1.0.0-rc.2
Cordova CLI : 6.5.0
Ionic CLI : 3.0.0-rc.2
本地包:
@ionic/app-scripts : 1.3.6
@ionic/cli-plugin-cordova : 1.0.0-rc.2
@ionic/cli-plugin-ionic-angular : 1.0.0-rc.2
Ionic Framework : ionic-angular 3.1.1
系统:
Node : v7.4.0
OS : macOS Sierra
Xcode : Xcode 8.3.2 Build version 8E2002
ios-deploy : 1.9.1
ios-sim : not installed
我会尝试制作一个单独的页面,我们有一个不同的控制器(用于该视图)。
首先,尝试以下故障排除以了解它是否真的在尝试获取请求:
转到 chrome->dev-tools->network 选项卡:在网络选项卡中,验证 angular 正在完成的 GET 请求。如果您看到它,请尝试查看请求的预览部分。有时,根据您的系统,会发生跨源阻止。有时只是您的 url,未被发现(404)或任何其他类型的错误。
其次:如果您看到请求但没有数据,可能是您在获取数据后正在做一些事情,比如从一个页面移动到另一个页面。尝试保存此类数据或尝试在 http 请求中创建所有逻辑。但是为什么在http请求里面呢?因为 http 本质上是异步的,你可能正在执行,而 observable 还没有得到响应才能做出反应。因此,您可能正在调用,但代码逻辑只是向前跳转。
我曾经尝试在我的项目的这个完全相同的区域中执行 http 请求,它们总是给我带来问题。
我为 Reddit 写了一个 http 请求,但没有在应用程序组件上工作 我在其他页面上复制了代码并且工作正常但是在应用程序组件上无法正常工作 Http 请求(和我的私人服务)
import { Http, Response,Request } from '@angular/http';
import 'rxjs';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
constructor(public http: Http,public agornaApi:AgornaApi ,private backgroundMode: BackgroundMode ,public localNotifications: LocalNotifications,platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, public storage: Storage,public authService: Auth) {
localNotifications.on("click", (notification, state) => {
this.nav.push(ListmessagesPage);
});
document.addEventListener('deviceready', function () {
// Android customization
// To indicate that the app is executing tasks in background and being paused would disrupt the user.
// The plug-in has to create a notification while in background - like a download progress bar.
cordova.plugins.backgroundMode.setDefaults({
title: 'بارگزاری',
text: ''
});
// Enable background mode
cordova.plugins.backgroundMode.enable();
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {
setInterval(function () {
this.http.get('https://www.reddit.com/r/gifs/new/.json?limit=10').map(res => res.json()).subscribe(data => {
cordova.plugins.notification.local.schedule({
text: data.kind,
data: { secret:'key' }
});
});
}, 10000);
}
}, false);
});
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
setTimeout(() => {
splashScreen.hide();
}, 100);
});
}
```
系统信息
全局包:
@ionic/cli-utils : 1.0.0-rc.2
Cordova CLI : 6.5.0
Ionic CLI : 3.0.0-rc.2
本地包:
@ionic/app-scripts : 1.3.6
@ionic/cli-plugin-cordova : 1.0.0-rc.2
@ionic/cli-plugin-ionic-angular : 1.0.0-rc.2
Ionic Framework : ionic-angular 3.1.1
系统:
Node : v7.4.0
OS : macOS Sierra
Xcode : Xcode 8.3.2 Build version 8E2002
ios-deploy : 1.9.1
ios-sim : not installed
我会尝试制作一个单独的页面,我们有一个不同的控制器(用于该视图)。
首先,尝试以下故障排除以了解它是否真的在尝试获取请求:
转到 chrome->dev-tools->network 选项卡:在网络选项卡中,验证 angular 正在完成的 GET 请求。如果您看到它,请尝试查看请求的预览部分。有时,根据您的系统,会发生跨源阻止。有时只是您的 url,未被发现(404)或任何其他类型的错误。
其次:如果您看到请求但没有数据,可能是您在获取数据后正在做一些事情,比如从一个页面移动到另一个页面。尝试保存此类数据或尝试在 http 请求中创建所有逻辑。但是为什么在http请求里面呢?因为 http 本质上是异步的,你可能正在执行,而 observable 还没有得到响应才能做出反应。因此,您可能正在调用,但代码逻辑只是向前跳转。
我曾经尝试在我的项目的这个完全相同的区域中执行 http 请求,它们总是给我带来问题。