react-native-background-timer: undefined is not an object (evaluating 'RNBackgroundTimer.setTimeout'

I’m getting this error:

simulator screen shot 28 jun 2017 16 33 57

This is the source code:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableOpacity,
} from 'react-native';


import BackgroundTimer from 'react-native-background-timer';

class WorkerSampleApp extends Component {


  render() {

    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={() => {

          // Start a timer that runs once after X milliseconds
          const timeoutId = BackgroundTimer.setTimeout(() => {
          	// this will be executed once after 10 seconds
          	// even when app is the the background
            	console.log('tac');

          }, 10000);

          // Cancel the timeout if necessary
          BackgroundTimer.clearTimeout(timeoutId);
          
        }}>
          <Text style={styles.welcome}>
            Send message
          </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});


AppRegistry.registerComponent('WorkerSampleApp', () => WorkerSampleApp);

Please help…

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 19 (3 by maintainers)

Most upvoted comments

I wanted to share that the link command alone did not help me, in the long run I had to link manually. In your src/main/java/com/appname/MainApplication.java

import com.ocetnik.timer.BackgroundTimerPackage;

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new VectorIconsPackage(),
          //........and so on
          new BackgroundTimerPackage()
      );
    }

good luck! android build errors in this ecosystem are frustrating sometimes 😃

Not working on expo!

I used react-native link and I checked src/main/java/com/appname/MainApplication.java for the correct links. Still getting the same error here… What might be going on?

Did you remember to run react-native link after installing the module? I ran into the same issue and running that command fixed it for me.

One more thing to the discussion: if you are using remote debugging via Wifi with a real phone, you still need to connect via cable. Remote reloading delivers only the Javascript files, while you need the new app build, which can only be delivered through a cable.

@the-simian thanks for your comment it did work for me as well now. Wondering why this is not properly working or in the Readme.

Works for me after adding the lines in MainApplication.java in addition to the react-native link