离子post请求不工作在后台模式



你好,我需要你的帮助,请我想发布地理位置数据,即使应用程序在后台运行时,我使用Cordova插件的get方法工作得很好,但我有一个问题与post

import { HttpClient } from '@angular/common/http';
import { Component, OnInit,OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { Geolocation } from '@capacitor/geolocation';
import { HTTP } from '@awesome-cordova-plugins/http/ngx';
import { BackgroundMode } from '@awesome-cordova-plugins/background-mode/ngx';

@Component({
selector: 'app-store-position',
templateUrl: './store-position.component.html',
styleUrls: ['./store-position.component.scss'],
})
export class StorePositionComponent implements OnInit,OnDestroy {
url = ' https://9564-197-2-220-171.ngrok.io/api/sharedPositions';
postData = {
sharedPositionCode:Date.now(),
sharedPositionName:this.router.getCurrentNavigation().extras.state.sharedPositionName,
longitude:"test",
latitude:"test",
};
currentPosition=[];
response:any;
interval:any;
visible=true;



constructor(private http:HttpClient,private router: Router,private httpIonic: HTTP,private backgroundMode: BackgroundMode) {
console.log(this.postData);
this.backgroundMode.enable();
}
ngOnDestroy(): void {
clearInterval(this.interval);
if (this.response.data.sharedPosition.id != null) {
this.http.delete(this.url+"/"+this.response.data.sharedPosition.id).toPromise().then(
data => {
console.log(data);

}
);
}


}
ngOnInit() {
console.log(this.postData.sharedPositionCode);

}



async saveSharedPosition(){
const currentPosition =Geolocation.getCurrentPosition({
enableHighAccuracy: true,
maximumAge: 1000,

});
this.backgroundMode.wakeUp();
this.httpIonic.get('https://9564-197-2-220-171.ngrok.io/api/sharedPositions', {}, {})
.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
this.postData.longitude = (await currentPosition).coords.longitude.toString();
this.postData.latitude = (await currentPosition).coords.latitude.toString();
this.currentPosition.unshift({ longitude:this.postData.longitude,latitude:this.postData.latitude})
console.log((await currentPosition).coords.accuracy);
this.httpIonic.post('https://9564-197-2-220-171.ngrok.io/api/sharedPositions',this.postData,{})
}
loopSaveSharedPosition() {
this.interval =  setInterval(() => {
this.saveSharedPosition()
}, 5000); 
this.visible=false;
}
}

对我来说唯一的问题是我使用了@angular/common/http和@awesome-cordova-plugins/http/ngx的post方法,但问题仍然存在

试试这个插件的背景地理定位。

当你想在后台运行一个服务时,你必须添加一个ForegroundService,检查那里

最新更新