ionic post request not working in background mode

Viewed 195

Hello I need your help please I want to post geolocation data even when app running in background I used Cordova plugin the get method work fine but I have an issue with 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;
}

}

the only issue for me is the post method i used both @angular/common/http and @awesome-cordova-plugins/http/ngx but the issue still remaining

1 Answers

Try this plugin for background geolocation.

When you want to run a service in background you have to add a ForegroundService, check there

Related