0

I am getting the pedometer value when I first start the app. But, after I close it and restart the app, the value is blank and there is no exception/error. Please review and advise. if you find any other working code please help me with that .I was using the https://github.com/voximplant/react-native-foreground-service for foreground services and expo pedometer for the pedometer. while using the pedometer only it works fine

export default class HomeScreen extends React.Component {
  
  state = {
    pastStepCount: 0,
  };

  componentDidMount() {
    this._subscribe();
  }

  componentWillUnmount(){
    this._unsubscribe();
  }

  _subscribe = () => {
    this._subscription = Pedometer.watchStepCount(result => {
      this.setState({
        currentStepCount: result.steps,
      });
    });
    try {
      this.setState();
      console.log("steps counting");
    } catch (error) {
      Alert.alert('error')
    }
  };
  _unsubscribe = () => {
    this._subscription && this._subscription.remove();
    this._subscription = null;
  };

  //start
  foregroundService = VIForegroundService.getInstance();

    state = {
        isRunningService: false,
    };
    componentDidMount(){
      this.startService();
    }
    componentWillUnmount(){
      this.stopService();
    }
    async startService() {
        if (this.state.isRunningService) return;
        if (Platform.Version >= 26) {
            const channelConfig = {
                id: 'ForegroundServiceChannel',
                name: 'Notification Channel',
                description: 'Notification Channel for Foreground Service',
                enableVibration: false,
                importance: 2
            };
            await this.foregroundService.createNotificationChannel(channelConfig);
        }
        const notificationConfig = {
            channelId: 'ForegroundServiceChannel',
            id: 3456,
            title: 'Foreground Service',
            text: 'Foreground service is running',
            icon: 'ic_notification',
            priority: 0,
        };
        try {
            this.subscribeForegroundButtonPressedEvent();
            await this.foregroundService.startService(notificationConfig);
            this.setState({isRunningService: true});
        } catch (error) {
            this.foregroundService.off();
        }
    }

    async stopService() {
        if (!this.state.isRunningService) return;
        this.setState({isRunningService: false});
        await this.foregroundService.stopService();
        this.foregroundService.off();
    }

    subscribeForegroundButtonPressedEvent() {
        this.foregroundService.on('VIForegroundServiceButtonPressed', async () => {
            await this.stopService();
        });
    }
  //ends

0 Answers0