react-native-background-timer icon indicating copy to clipboard operation
react-native-background-timer copied to clipboard

Not working when app on background (ios and android)

Open addingama opened this issue 6 years ago • 12 comments

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

addingama avatar Mar 05 '18 05:03 addingama

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>

erick2014 avatar Mar 07 '18 21:03 erick2014

@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

addingama avatar Mar 07 '18 23:03 addingama

@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 avatar Mar 09 '18 14:03 erick2014

@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

erick2014 avatar Mar 09 '18 15:03 erick2014

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

domozy87 avatar Mar 14 '18 10:03 domozy87

@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.

stachu2k avatar Apr 04 '18 17:04 stachu2k

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?

patrickalbani avatar Apr 11 '18 12:04 patrickalbani

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 avatar Aug 30 '18 02:08 bportman

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.

bportman avatar Sep 18 '18 03:09 bportman

please upgrade module yarn upgrade [email protected]

wdayanand avatar Oct 08 '18 13:10 wdayanand

Not working

shafiqshams avatar Nov 14 '18 11:11 shafiqshams

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 both BackgroundTimer.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.

chuckntaylor avatar Jan 09 '19 19:01 chuckntaylor