Error BackgroundFetch.configure is not a function while using in Android App
Hi, I am trying to use nativescript-background-fetch in Android App but it gives me error that BackgroundFetch.configure is not a function. I am using Javascript to develop android native app. My code in home-view-model.js is as below:
const observableModule = require("data/observable");
var permissions = require('nativescript-permissions');
var BackgroundFetch = require("nativescript-background-fetch");
function HomeViewModel() {
const viewModel = observableModule.fromObject({
/* Add your view model properties here */
onButtonTap: function () {
console.log(BackgroundFetch);
BackgroundFetch.configure({
minimumFetchInterval: 15, // minutes
stopOnTerminate: false, // Android-only
startOnBoot: false // Android-only
}, () => {
console.log("[js] BackgroundFetch event received");
//
// Do stuff. You have 30s of background-time.
//
// When your job is complete, you must signal completion or iOS can kill your app. Signal the nature of the fetch-event, whether you recevied:
// FETCH_RESULT_NEW_DATA: Received new data from your server
// FETCH_RESULT_NO_DATA: No new data received from your server
// FETCH_RESULT_FAILED: Failed to receive new data.
BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
}, (status) => {
console.log('BackgroundFetch not supported by your OS', status);
});
console.log("Button was pressed");
},
textFieldValue: "",
});
return viewModel;
}
module.exports = HomeViewModel;
I see I haven't provided an ISSUE_TEMPLATE for this repo. Please provide the following information:
Your Environment
- Plugin version:
- Platform: iOS or Android
- OS version:
- Device manufacturer / model:
- Nativescript version (
tns info): - Plugin config
Hi Chris, here is the Environment info:
- Plugin version: 1.2.0
- Platform: Android
- OS version: 6.0
- Device manufacturer / model: HUAWEI VNS-L22
- Nativescript version (tns info): 4.1.3
- Plugin config : Added in code
Change this:
var BackgroundFetch = require("nativescript-background-fetch");
to this:
import {BackgroundFetch} from "nativescript-background-fetch";
Changing to below line gives error "unexpected token {" (its a opening curly bracket after import) on load of that page and view is not rendered.
import {BackgroundFetch} from "nativescript-background-fetch";
are you not using typescript?
No.
try this:
var BackgroundFetch = require("nativescript-background-fetch").BackgroundFetch;
Now, above errors are resolved but getting new error:
TypeError: Cannot read property 'Builder' of undefined. File: /nativescript-background-fetch/background-fetch.js, line 27, Column 38
I've created a new blank app (without typescript) and it works.
$ tns create Fetch
$ tns plugin add nativescript-background-fetch
$ tns info
✔ Getting NativeScript components versions information...
✔ Component nativescript has 4.1.2 version and is up to date.
✔ Component tns-core-modules has 4.1.0 version and is up to date.
✔ Component tns-android has 4.1.3 version and is up to date.
✖ Component tns-ios is not installed.
main-view-model.js
var Observable = require("data/observable").Observable;
var BackgroundFetch = require("nativescript-background-fetch").BackgroundFetch;
function getMessage(counter) {
if (counter <= 0) {
return "Hoorraaay! You unlocked the NativeScript clicker achievement!";
} else {
return counter + " taps left";
}
}
function createViewModel() {
var viewModel = new Observable();
viewModel.counter = 42;
viewModel.message = getMessage(viewModel.counter);
BackgroundFetch.configure({
minimumFetchInterval: 15
}, function() {
console.log('- NativeScript BackgroundFetch Received');
}, function() {
console.log('- NativeScript BackgroundFetch error');
});
viewModel.onTap = function() {
this.counter--;
this.set("message", getMessage(this.counter));
}
return viewModel;
}
exports.createViewModel = createViewModel;