ocetnik / react-native-background-timer

Emit event periodically (even when app is in the background)
MIT License
1.61k stars 225 forks source link

Not working when app on background (ios and android) #78

Open addingama opened 6 years ago

addingama commented 6 years ago

Hi @ocetnik,

I've been trying to use this library but it not working when the app put on background.

I've followed all installation guide and cross platform usage but it still not working.

I only test it on emulator for both on ios and android. Is there something that not documented?

This is the env

Environment:
  OS: macOS High Sierra 10.13.3
  Node: 9.6.1
  Yarn: 1.5.1
  npm: 5.6.0
  Watchman: 4.9.0
  Xcode: Xcode 9.2 Build version 9C40b
  Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed)
  react: 16.0.0 => 16.0.0
  react-native: 0.51.0 => 0.51.0
  react-native-background-timer: 2.0.1
erick2014 commented 6 years ago

Hi @dashracer could you please share how you did the installation?

I'm getting this error:

React/RCTBridgeModule.h' file not found
#import <React/RCTBridgeModule.h>
addingama commented 6 years ago

@erick2014 I only follow installation step from this repo and make sure Podfile updated. After that I trigger pod install manually just to make sure the library installed on iOS

I never get that error, you can try to clean then build again from xcode

erick2014 commented 6 years ago

@dashracer thanks for your help, at the end I did this, to compile it in ios:

1. cd ios
2. Added these dependencies to the Podfile:
pod 'React', :path => '../node_modules/react-native', :subspecs =>[
    'DevSupport',
    'Core',
    'RCTNetwork'
]
pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
pod 'react-native-background-timer', :path => '../node_modules/react-native-background-timer'
pod 'Google/SignIn'

3. pod install
erick2014 commented 6 years ago

@dashracer I'm testing it on ios, and I did something like :

 componentDidMount() {
    BackgroundTimer.start()
    this.interval = setInterval(() => {
      console.log('okay baby')
    }, 10000);
  }

  stopTimer = () => {
    clearInterval(this.interval);
    BackgroundTimer.stop()
  }

and I checked the console.log in background and seems to work

domozy87 commented 6 years ago

@erick2014 - I have tested, and it is not working in the background. Once the app is put into the background, the timer stops.

stachu2k commented 6 years ago

@erick2014 BackgroundTimer.start() and BackgroundTimer.stop() not working for me. The code where i have setTimeout just wait when app is brought back to the foreground, It should be run after 10 seconds, and it does but only when the app is in the foreground. In the background after 10 seconds nothing happens until i bring it back to the foreground. Android's solution BackgroundTimer.setTimeout works perfectly, but iOS's solutions not working for me.

patrickalbani commented 6 years ago

I have tested in Android and only work if connected to PC. In my case i send a graphql mutation every minute, but when i disconnect from pc, stop sending. Any suggestions?

bportman commented 6 years ago

I solved this issue in debug with this: https://github.com/ocetnik/react-native-background-timer/issues/9#issuecomment-291562476

Crossing my fingers it works in release configuration as well.

bportman commented 5 years ago

That was not a good fix - because it relies on playing silent audio in the background, the timer stops after 3 minutes as soon as a user plays any sound in another app.

wdayanand commented 5 years ago

please upgrade module yarn upgrade react-native-background-timer@2.1.0-alpha.5

shafiqshams commented 5 years ago

Not working

chuckntaylor commented 5 years ago

I seem to have got this up and running, so I hope this helps others.

Packages:
  "react": "16.6.3",
    "react-native": "0.57.8",
    "react-native-background-timer": "^2.1.1",

I installed react-native-background-timer via yarn add react-native-background-timer

And then ran the command react-native link react-native-background-timer

I was using the documented case for Android, but it worked in both iOS and Android for me when the app was in the background.

BackgroundTimer.setInterval()
BackgroundTimer.clearInterval()

However, if the phone was switched off, in iOS, the timer would pause until the phone was turned on again. It seemed fine in the simulator, but when running on the actual device I would get this pausing behaviour when the phone was off.

I ended up combining what I was doing with what @erick2014 mentioned above. In my case, I am building a little timer app for meditation, so I have the timer stop at 0 seconds.

import React from 'react'
import { View, Text } from 'react-native'
import BackgroundTimer from 'react-native-background-timer'

let timer = 0

class Timer extends React.Component {
state = {
seconds: 30
}

componentDidMount() {
    this.runTimer()
  }

  componentWillUnmount() {
    this.stopTimer()
  }

runTimer = () => {
    BackgroundTimer.start()
    timer = BackgroundTimer.setInterval(() => {
      if (this.state.seconds === 0) {
        BackgroundTimer.clearInterval(timer)
        BackgroundTimer.stop()
        return
      }
      this.setState({
        seconds: this.state.seconds - 1
      })
    }, 1000)
  }

  stopTimer = () => {
    BackgroundTimer.clearInterval(timer)
    BackgroundTimer.stop()
    //BackgroundTimer.clearInterval(timer)
  }

render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Timer Run Screen</Text>
        <Text>{`${this.state.seconds} seconds`}</Text>
      </View>
    )
  }

}

export default Timer

So essentially, I just used bothBackgroundTimer.start() with BackgroundTimer.setInterval(), and BackgroundTimer.stop() with BackgroundTimer.clearInterval(). I also setup the timer variable to hold the intervalId at the top just under my imports let timer = 0, although this could be moved into the class.