How to call rest api in background mode in ionic 4+

How to call rest api in background mode in ionic 4+

platform.ready().then(async () => {
console.log(“background mode offff”)
this.backgroundMode.enable();
console.log(“background mode check”,this.backgroundMode.isActive(), this.backgroundMode.isEnabled(),this.backgroundMode.isScreenOff);

  // this.backgroundMode.enable();

  console.log("-------------------->", this.backgroundMode.isEnabled());
  console.log("-------------------->", this.backgroundMode.isActive());
  this.backgroundMode.on("activate").subscribe(async ()=>{
    // alert("background mode activated")
    this.getLastestOrders()
    console.log("background mode on");

    setInterval(()=>{
      console.log("calling in 2 sec")
    }, 2000);
  });

  
});

Description:-

only shows on ‘background mode on’ … setInterval is not working, and this.getLastestOrders() is also not calling here

what wrong in my code ??

Background tasks/mode are generally not for making regular potentially infinite operations, and can lead to app store rejections. Based on what you are trying to do, I would look into web sockets instead, and have the server push new orders to the app.

1 Like

I appreciate your answer,
can you provide sample code for WebSockets in ionic 4+ and nodejs

its a request.

Don’t have sample code, but this is the lib I use with Node.js.

and it will connect to ionic ?