transistorsoft / nativescript-background-fetch

iOS Background Fetch API Implementation for NativeScript
Other
28 stars 9 forks source link

Error BackgroundFetch.configure is not a function while using in Android App #9

Open TarakShah17 opened 6 years ago

TarakShah17 commented 6 years ago

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;
christocracy commented 6 years ago

I see I haven't provided an ISSUE_TEMPLATE for this repo. Please provide the following information:

Your Environment

TarakShah17 commented 6 years ago

Hi Chris, here is the Environment info:

christocracy commented 6 years ago

Change this:

var BackgroundFetch = require("nativescript-background-fetch");

to this:

import {BackgroundFetch} from "nativescript-background-fetch";
TarakShah17 commented 6 years ago

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";

christocracy commented 6 years ago

are you not using typescript?

TarakShah17 commented 6 years ago

No.

christocracy commented 6 years ago

try this:

var BackgroundFetch = require("nativescript-background-fetch").BackgroundFetch;
TarakShah17 commented 6 years ago

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

christocracy commented 6 years ago

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;