How can I make a request every x seconds with the background services plugin?

I am using this plugin and although I have managed to start a service I still do not deal very well with old or poorly documented plugins. For example how could I do that using this plugin and with a background service make a GET request or an Alert or something that I can see from the mobile every “x” seconds.

This is the plugin and a sample


Why do you want to do that?

Looking at the plugin and sample you would have to write whatever functionality you want to have in the doWork method: bgs-sample/ at master · Red-Folder/bgs-sample · GitHub and then just start and time the service with the functionality offered by the base plugin.

It’s actually pretty well explained at Using the MyService Sample · Red-Folder/bgs-core Wiki · GitHub

Because i want to make a get petition even the app is closed and if i detect changes on that JSON send a local notification or some kind of callback

Elaborate. What kind of JSON with what kind of data?

I have a local API who returns this type of JSON when you calls him through an http petition:

"city": {
"wall": 0,
"log: 0,
"class": 0
"town": {
"wall": 0,
"log": 0,
"class": 0
"country": {
"wall": 0,
"log": 0,
"class": 0

If i have changes on that values i wanna send a notification or some type of alert to the user

Why don’t you just send a notification from the server to all devices instead of doing the same logic and checking on all devices?

Are you talking about push notifications? I can’t, i need to achieve this with a service and i don’t even found another plugins or stuff to do this

Yes, I talk about push notifications. What you describe is the perfect use case for them.
Why do you think you need a service for this?

Services are hard. They tend to suck because they run in the background all the time, which is why they are so limited on both mobile OS platforms. That is also why not many people (successfully) try to do this with Cordova.

Because they force me, it’s not my idea. Maybe I should start this project in native, but it’s late and I have the time over. Thanks for the help and sorry to be so heavy

In native you have to implement the exact same logic for the service as you now have to with the bgs-core thingie - I linked to the method etc above - didn’t look too complicated. And it will suck in all the same ways (for development and later users).

The method doWork() it’s seems doesn’t work properly… I don’t know how refactorize the logic of this.


Does the example work?

Yes, the example works, but i’m don’t know how do my own logic. Im missed

What exactly should your own logic be and do? You will have to write that in Java for the native service to work.

I want to do an http request while the program is closed and check the JSON above

Then you will haev to write Java code for these:

  1. Get a file from a URL
  2. Compare JSON you got to something you already have
  3. Do things that should be done if conditions are met

But this is not really a Ionic problem any more - you will probably have more luck on a Cordova forum or chat or someting where Android developers hang out. We all use Ionic to not have to handle such stuff :wink:

Yes, i agreed with that, i’m not confortable when i exit of my pleasure burble of Ionic xD

Can i make you another request? How can i include the typings of an external library?