vikeri / react-native-background-job

Schedule background jobs in React Native that run your JavaScript when your app is in the background/killed.
MIT License
747 stars 109 forks source link

None tasks are working in background after app killed in Android 10 #166

Open yadeepak opened 4 years ago

yadeepak commented 4 years ago
"dependencies": {
    "@react-native-community/async-storage": "^1.12.0",
    "react": "16.13.1",
    "react-native": "0.63.2",
    "react-native-background-job": "^2.3.1"
  },

for code, I am using example of this repository

import React, { Component } from "react";
import {
  AppRegistry,
  TouchableHighlight,
  StyleSheet,
  Text,
  View
} from "react-native";

import BackgroundJob from "react-native-background-job";

const regularJobKey = "regularJobKey";
const exactJobKey = "exactJobKey";
const everRunningJobKey = "everRunningJobKey";

BackgroundJob.register({
  jobKey: regularJobKey,
  job: () => console.log(`Background Job fired!. Key = ${regularJobKey}`)
});
BackgroundJob.register({
  jobKey: exactJobKey,
  job: () => {
    console.log(`${new Date()}Exact Job fired!. Key = ${exactJobKey}`);
  }
});
BackgroundJob.register({
  jobKey: everRunningJobKey,
  job: () => console.log(`Ever Running Job fired! Key=${everRunningJobKey}`)
});

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = { jobs: [] };
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Testing BackgroundJob</Text>
        <Text style={styles.instructions}>
          Try connecting the device to the developer console, schedule an event
          and then quit the app.
        </Text>
        <Text>
          Scheduled jobs:
          {this.state.jobs.map(({ jobKey }) => jobKey)}
        </Text>
        <TouchableHighlight
          style={styles.button}
          onPress={() => {
            BackgroundJob.schedule({
              jobKey: regularJobKey,
              notificationTitle: "Notification title",
              notificationText: "Notification text",
              period: 15000
            });
          }}
        >
          <Text>Schedule regular job</Text>
        </TouchableHighlight>
        <TouchableHighlight
          style={styles.button}
          onPress={() => {
            BackgroundJob.schedule({
              jobKey: exactJobKey,
              period: 1000,
              exact: true
            });
          }}
        >
          <Text>Schedule exact job</Text>
        </TouchableHighlight>
        <TouchableHighlight
          style={styles.button}
          onPress={() => {
            BackgroundJob.isAppIgnoringBatteryOptimization(
              (error, ignoringOptimization) => {
                if (ignoringOptimization === true) {
                  BackgroundJob.schedule({
                    jobKey: everRunningJobKey,
                    period: 1000,
                    exact: true,
                    allowWhileIdle: true
                  });
                } else {
                  console.log(
                    "To ensure app functions properly,please manually remove app from battery optimization menu."
                  );
                  //Dispay a toast or alert to user indicating that the app needs to be removed from battery optimization list, for the job to get fired regularly
                }
              }
            );
          }}
        >
          <Text>Schedule ever running job</Text>
        </TouchableHighlight>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  button: { padding: 20, backgroundColor: "#ccc", marginBottom: 10 },
  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("backgroundjob", () => App);

If I am wrong anywhere point me

parmarkamlesh commented 3 years ago

@yadeepak same issue, any update? when app is removed from recent it wont trigger any update, works fine when app is in memory