transistorsoft / cordova-plugin-background-fetch

Implements background fetching of data.
MIT License
296 stars 79 forks source link

backgroundFetch does not get called on the background #70

Closed HamzaLJ closed 7 years ago

HamzaLJ commented 7 years ago

I've the below :

     bkFetch(){
      const config: BackgroundFetchConfig = {
        stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true.
      };

      this.backgroundFetch.configure(config)
         .then(() => {
             console.log('Background Fetch initialized');

             this.backgroundFetch.finish();

         })
         .catch(e => console.log('Error initializing background fetch', e));

         console.log('background fetch is working')

      this.backgroundFetch.start();

    }

However, when calling it on the constructor when the app opens for the first time (foreground), when opening another app, nothing gets printed in the console.

      this.platform.ready().then((ready) => {
         this.bkFetch();
      });

Am I doing something wrong ?

christocracy commented 7 years ago

You cannot predict when iOS background fetch event will fire.

Just simulate a background fetch event with xCode. If it fires, you're good.

It can take a few days before you see regular events fire on a real device.

See #65

HamzaLJ commented 7 years ago

Got it, so basically it's not like Android. In my case, I want to update the backend each x seconds, in my case I guess it's not possible. Is that right ?

christocracy commented 7 years ago

iOS is very strict with background operation, for good reason. You just don't get to do whatever you wish. You would kill the user's battery.

On Thu, Aug 24, 2017 at 7:00 PM Hamza L notifications@github.com wrote:

Got it, so basically it's not like Android. In my case, I want to update the backend each x seconds, in my case I guess it's not possible. Is that right ?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/transistorsoft/cordova-plugin-background-fetch/issues/70#issuecomment-324780169, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6lzZPB6t7X8fpzTDVHd8KiyQyF2sfks5sbgCQgaJpZM4PB_vi .

-- Snet form Gmail Mobile

HamzaLJ commented 7 years ago

That's true! So, in my case this plugin will not be helpful to perform the task needed, right?

christocracy commented 7 years ago

If your hope is to do something every x seconds on iOS, it's impossible.

On Thu, Aug 24, 2017 at 7:04 PM Hamza L notifications@github.com wrote:

That's true! So, in my case this plugin will not be helpful to perform the task needed, right?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/transistorsoft/cordova-plugin-background-fetch/issues/70#issuecomment-324780787, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6ly3-0JKZ2yuT_sXWgDP8z-x2KvIeks5sbgGHgaJpZM4PB_vi .

-- Snet form Gmail Mobile

HamzaLJ commented 7 years ago

Will it be possible to update the database in the background if I try to put the update() function inside Background Geolocation plugin function ?

repoissp commented 7 years ago

I'm trying to make the background fetch to work....it's a good idea to simulate the background fetch with xCode to see if it works as indicated by @christocracy . I don't understand where should I provide the callbackFn to make the ionic native plugin work...could you @HamzaLJ explain if you were able to make it work, and where did you provide the function to be executed? the doc says in the configuration object, but it only gets one parameter which is 'stopOnTerminate'. Thanks in advance!

christocracy commented 7 years ago

@repoissp is there something unclear about this:

var Fetcher = window.BackgroundFetch;

// Your background-fetch handler.
var fetchCallback = function() {
  console.log('[js] BackgroundFetch initiated');

  Fetcher.finish();
}
var failureCallback = function(error) {
  console.log('- BackgroundFetch failed', error);
};
Fetcher.configure(fetchCallback, failureCallback, {
  stopOnTerminate: false  // <-- true is default
});
repoissp commented 7 years ago

Thanks @christocracy for your reply. I'm trying to make this work on Ionic 3. Using the 'ionic-native/background-mode' wrapper. The configuration parameter that "this.backgroundFetch.configure(config)" gets, only allows one parameter which is 'stopOnTerminate'. That's why I can't find a way to provide the callbackFn to execute. Thanks in advance!

christocracy commented 7 years ago

Using the 'ionic-native/background-mode' wrapper

This plugin has nothing to do with "background-mode". Forget about wrappers.

let Fetcher = (<any>window).BackgroundFetch;

Then follow the example above.

germanurrus commented 7 years ago

Thanks @christocracy I was able to make it run on my Xcode emulator with your guidance. Is there any way to force its execution on an real iOS device..or al least a way to know that everything is configured ok...since so far it's not doing any fetch request, although I was able to run it on emulator as I said. Thanks a lot again.

christocracy commented 7 years ago

If it works in sim, it'll work on device.

There's not much to this API. It's basically an on/off button. There's very little code involved in this plugin.

On Sat, Sep 9, 2017 at 3:25 PM germanurrus notifications@github.com wrote:

Thanks @christocracy https://github.com/christocracy I was able to make it run on my Xcode emulator with your guidance. Is there any way to force its execution on an real iOS device..or al least a way to know that everything is configured ok...since so far it's not doing any fetch request, although I was able to run it on emulator as I said. Thanks a lot again.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/transistorsoft/cordova-plugin-background-fetch/issues/70#issuecomment-328298438, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6l3lL2W0X-ZvFXUNvvY_V3OHQcarTks5sguY6gaJpZM4PB_vi .

-- Snet form Gmail Mobile

faldunate commented 7 years ago

Hello @christocracy I have a doubt, What does it really mean?..

If it works in sim, it'll work on device?

I ask you this, because in my app background fetch is running in simulator and this is my log when the App is running in simulator:

Hello BackgroundFetchProvider Provider
[TSBackgroundFetch configure]: {
    stopOnTerminate = 0;
}
[TSBackgroundFetch start]
[TSBackgroundFetch addListener]: CDVBackgroundFetch
THREAD WARNING: ['BackgroundFetch'] took '61.634277' ms. Plugin should use a background thread.
CDVBackgroundFetch Rx Fetch Event
THREAD WARNING: ['HealthKit'] took '17.795166' ms. Plugin should use a background thread.
[TSBackgroundFetch doFinish] Complete, UIBackgroundFetchResult: 0, responses: 1

When I run the app from xcode this work it, and does what it has to do, but background fetch is not running periodically.

If background fetch run only one time... I can assume that Background Fetch in the device will run periodically?

christocracy commented 7 years ago

Apple uses a secret algorithm to determine the frequency of fetch events. This algorithm is presumably based upon app usage patterns (ie: how frequently and when the user users the app). It can take several days before fetch events begin to arrive consistently.

If fetch event works in simulator, that proves that everything is correctly set up. There's nothing you can do but wait.

faldunate commented 7 years ago

@christocracy thank you for your response, In simulator I need to wait to fetch events begin to arrive consistently? or it happen only in devices?. All my tests I'm doing are in simulator, and only execute the fetch event when I run app in simulator.

My question is... in simulator, Should I have an interval of 15 minutes?

christocracy commented 7 years ago

Yes, you need to wait. This plugin is an ON/OFF switch. If you see simulated fetch events fire, the plugin is ON.

There's nothing more to do. Nothing.