Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TarakShah17 opened this issue Jul 5, 2018 · 9 comments

Comments

@TarakShah17
Copy link

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
Copy link
Member

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

@TarakShah17
Copy link
Author

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

@christocracy
Copy link
Member

christocracy commented Jul 5, 2018

Change this:

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

to this:

import {BackgroundFetch} from "nativescript-background-fetch";

@TarakShah17
Copy link
Author

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
Copy link
Member

are you not using typescript?

@TarakShah17
Copy link
Author

No.

@christocracy
Copy link
Member

try this:

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

@TarakShah17
Copy link
Author

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
Copy link
Member

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants