Reputation: 499
I would like to implement this plugin: https://github.com/christocracy/cordova-plugin-background-fetch. I installed the plugin using CLI, then copied the BackgroundFetch.js file to my www directory and referenced it in index.html. I placed the sample code as given in the git url in the DeviceReady function. Still, on startup I am faced with the error:
TypeError: 'undefined' is not an object (evaluating 'Fetcher.configure')
Can someone please tell me why this is happening? I've been trying for two days now but to no avail. Any help will be GREATLY appreciated. Thanks in advance!
After seeing Clawish's suggestion, I removed the js file and the reference to it, and removed and added the plugin again. Now I don't get any errors, but I don't see "CDVBackgroundFetch configure" in the logs. If I simulate a background fetch on the simulator, I get the error: -
CDVBackgroundFetch onFetch
Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.
Where am I going wrong? I haven't made any changes to the code except adding the sample code to call Fetcher.configure in onDeviceReady
After seeing your Clawfish's second edit, I removed the service call and shortened the function as below:
onDeviceReady: function() {
app.receivedEvent('deviceready');
var Fetcher = window.plugins.backgroundFetch;
// Your background-fetch handler.
var fetchCallback = function() {
console.log('BackgroundFetch initiated');
Fetcher.finish();
}
Fetcher.configure(fetchCallback);
},
Still, I don't see any log message saying "CDVBackgroundFetch configure" and on simulating Background fetch the same warning comes as above. It seems the Fetch part is being executed without it running configure in the first place.
Upvotes: 5
Views: 1542
Reputation: 2964
Edit2: I guess you have added the whole sample code from the docs (with decalaration of var Fetcher
and the handler var fetchCallback
and after declaring both, you run Fetcher.configure(fetchCallback);
.
The problem might be that you don't have jQuery referenced in your index.html
and the code stops running when it sees the line $.get(...)
. If that is the case, download jQuery, put it into your www
folder, and reference it from your index.html
.
Do you see the console message console.log('BackgroundFetch initiated');
? What about /heartbeat.json
, did you change that?
Edit: You don't need to copy the BackgroundFetch.js to your www
directory, and you also don't need to reference it in your index.html
. Try to remove the reference in your index.html
and leave the plugin files untouched after installing. If you have changed something regarding the file structure you should cordova plugin remove
the plugin and add
it again afterwards.
I don't know from which console you are recieving this error. If it is a browser console, like Chrome, then no wonders, since the Fetcher does only work on an iDevice (or iOS-simulator). And the Cordova Console plugin does not print such messages.
Try to use the code in the iOS simulator.
Upvotes: 1