14

I am trying to figure out how to run background service in react native (Android). Is it possible to run code (e.g. socket listener) while app is running in background or not. I created something simple to determine that, but seems like it's not working that way. Any help?

    class HolaProject extends React.Component {

    constructor(props) {
      super(props);
      this.state = {holaText: "0"};
      var self = this;

      setTimeout(() => {
        self.setState({holaText: "after 10 seconds, works!"})
      }, 10000);
    }

    render() {
      return (
        <DrawerLayoutAndroid renderNavigationView={() => <Text>React Native</Text>}>
        <Text>{this.state.holaText}</Text>
        <ProgressBarAndroid />
      </DrawerLayoutAndroid>
    );
  }
};
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Haris Bašić
  • 1,383
  • 2
  • 12
  • 21

1 Answers1

1

On Android

Yes, it can be done. Check this link for more information. Basically, you have to enable a notification to "wake" the device when receiving information.

On iOS

Yes, it can be done too, but through hacks. Check these links on SO to have a deeper understanding of these hacks

Play a silent sound and set the background mode to audio. Check this answer for more information on the matter.

Use VoIP services. Check more here but it'll require you justify a VoIP service to Apple.

Community
  • 1
  • 1
G. Hamaide
  • 7,078
  • 2
  • 36
  • 57