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 108 forks source link

BackgroundJob not working when app is kill/close... #110

Open bajrangsharma-burrp opened 5 years ago

bajrangsharma-burrp commented 5 years ago

Issue Description

Unable to run background job even app is closed

Steps to Reproduce / Code Snippets / Screenshots

1> Launch the app, 2> Do some activities, 3> Kill/Close the app by swipe

Environment

React Native version: "react-native": "0.52.0", "react-native-background-job": "^2.1.1",

Platform(s) (iOS, Android, or both?):

Android

Device info (Simulator/Device? OS version? Debug/Release?):

Device-7.1.1-Debug

bajrangsharma-burrp commented 5 years ago

waiting for nice update...

vikeri commented 5 years ago

Try the latest version 2.2.0

bajrangsharma-burrp commented 5 years ago

Thanks for the update...Sure will try...

bajrangsharma-burrp commented 5 years ago

Updated 2.2.0 ("react-native-background-job": "^2.2.0") but still same issue...

vikeri commented 5 years ago

Can you show me your code?

bajrangsharma-burrp commented 5 years ago

Code implementation as below:

const backgroundJob = {
   jobKey: "myJob",
   job: () => { 
     locat(); 
    }
  };

 BackgroundJob.register(backgroundJob);

 var backgroundSchedule = {
  jobKey: "myJob",
  period: 2000,
  allowWhileIdle: true,
  exact: true,
  requiresCharging: true,
  requiresDeviceIdle: true,
  override: false,
  timeout: 3000,
  allowExecutionInForeground: true,
  networkType: BackgroundJob.NETWORK_TYPE_ANY
 }

 BackgroundJob.schedule(backgroundSchedule);

 BackgroundJob.isAppIgnoringBatteryOptimization((error,ignoringOptimization) => { 
   console.log(error+' <<Android>>ignore>> '+ignoringOptimization) 
  });

const locat = async (data) => {
  navigator.geolocation.getCurrentPosition((position) => {
    console.log('Android>>: Location>>222--->>: '+JSON.stringify(position.coords));
   });
vikeri commented 5 years ago

Could you try with a function that is not async and try with only the required options to see if that works

bajrangsharma-burrp commented 5 years ago

Try but same issue. Close app by swipe then nothing is running in background. How can be achieve this goal???...

vikeri commented 5 years ago

Can I see the updated code that you tried?

bajrangsharma-burrp commented 5 years ago
const locat = (data) => {
navigator.geolocation.getCurrentPosition((position) => {
console.log('Android>>: Location>>222--->>: '+JSON.stringify(position.coords));
});

removed async. Working fine when app in Foreground and Background(not kill/close)...

bajrangsharma-burrp commented 5 years ago

Any hope for the same implementation logic...

vikeri commented 5 years ago

Try these settings instead

var backgroundSchedule = {
jobKey: "myJob",
period: 2000,
exact: true,
allowExecutionInForeground: true,
}

Also, be aware that it can take a long while before the apps starts up again depending on when Android lets your app start up.

bajrangsharma-burrp commented 5 years ago

Sure will try the same...

bajrangsharma-burrp commented 5 years ago

Its working but not all devices...Don`t know why?.

Working on Samsung after kill/close the app but not working on OPPO.

Is there any specific reason?.

I think OPPO device OS could be customised so not allowed any background services...Don`t know m correct or wrong.

If u have any clarification Plz share with me...

bajrangsharma-burrp commented 5 years ago

Yesterday this logic working fine on Android version 8.0(Oreo) but today not working on kill/close.

This scenario is so strange still don`t understand...In which case this is working and in which case this is not working on Oreo and why?.

Let me know behind of this actually what happened?...

guitar9 commented 5 years ago

does not work with galaxy s7. After killing the app the job does not execute. Scheduling push notification works with this libaray https://github.com/zo0r/react-native-push-notification, but there is no event when the push notification shows. In this Libary they use the Alarm Manager.

bajrangsharma-burrp commented 5 years ago

hi,

After new install "react-native-background-job" getting error:

Re-install...cache clean...delete build folder did everything but issue present.

Plz let me something why this happened?...

bajrangsharma-burrp commented 5 years ago

Any hope for Android Oreo OS...

bajrangsharma-burrp commented 5 years ago

Hi,

This logic working on all devices with Oreo(Android OS v8.0+) but not working on OPPO(7.1.1) device after kill/close but at Samsung(7.1.1) working fine.

Wts going behind of these Plz let me and why this happened...

abtuhin commented 5 years ago

This works in app closed/background/foreground

import React, {Component} from 'react';
import {Platform, TouchableOpacity, Text, View} from 'react-native';
import BackgroundJob from 'react-native-background-job';
import axios from "axios";

const backgroundJob = {
  jobKey: "myJob",
  job: async () => {
    try{
      let res = await axios.get("https://jsonplaceholder.typicode.com/posts/42")
      console.log(res.data)
    }catch(e){
      console.log(e);
    }

  }
 };

BackgroundJob.register(backgroundJob);

export default function App(){

  const startJob = () => {
    BackgroundJob.schedule({
      jobKey: "myJob",
      period: 5000,
      exact: true,
      allowExecutionInForeground: true,
    });
  }

  return (
    <View style={{flex:1}}>
      <TouchableOpacity onPress={startJob}>
        <Text>Start</Text>
      </TouchableOpacity>
    </View>
  );
}
JohnTheo13 commented 5 years ago

@abtuhin it works for 15 minutes of inactivity, but after that it stops. I removed the app from the battery optimisation settings but still the same. Any ideas?

rasheedk commented 4 years ago

Use this to check weather battery optimization is enabled ?

https://www.npmjs.com/package/react-native-disable-battery-optimizations-android

Mangaleswarisakthi commented 4 years ago

This works in app closed/background/foreground


import React, {Component} from 'react';
import {Platform, TouchableOpacity, Text, View} from 'react-native';
import BackgroundJob from 'react-native-background-job';
import axios from "axios";

const backgroundJob = {
  jobKey: "myJob",
  job: async () => {
    try{
      let res = await axios.get("https://jsonplaceholder.typicode.com/posts/42")
      console.log(res.data)
    }catch(e){
      console.log(e);
    }

  }
 };

BackgroundJob.register(backgroundJob);

export default function App(){

  const startJob = () => {
    BackgroundJob.schedule({
      jobKey: "myJob",
      period: 5000,
      exact: true,
      allowExecutionInForeground: true,
    });
  }

  return (
    <View style={{flex:1}}>
      <TouchableOpacity onPress={startJob}>
        <Text>Start</Text>
      </TouchableOpacity>
    </View>
  );
}
`the same way i used background service after app killing and sleeping mode background service worked android version 6 but the same way not worked android version 9 
showtan001 commented 4 years ago

This works in app closed/background/foreground

import React, {Component} from 'react';
import {Platform, TouchableOpacity, Text, View} from 'react-native';
import BackgroundJob from 'react-native-background-job';
import axios from "axios";

const backgroundJob = {
  jobKey: "myJob",
  job: async () => {
    try{
      let res = await axios.get("https://jsonplaceholder.typicode.com/posts/42")
      console.log(res.data)
    }catch(e){
      console.log(e);
    }

  }
 };

BackgroundJob.register(backgroundJob);

export default function App(){

  const startJob = () => {
    BackgroundJob.schedule({
      jobKey: "myJob",
      period: 5000,
      exact: true,
      allowExecutionInForeground: true,
    });
  }

  return (
    <View style={{flex:1}}>
      <TouchableOpacity onPress={startJob}>
        <Text>Start</Text>
      </TouchableOpacity>
    </View>
  );
}

This is not a code reason, you know? This is the reason for different devices and Android systems.

dinesh14ahuja commented 3 years ago

Hello All, Can I define my job with some parameters?

const backgroundJob = {
  jobKey: "myJob",
  job: async (parameter) => {
   //logic using parameter
  }
 };
jsriganeshgdt commented 3 years ago

This works in app closed/background/foreground

import React, {Component} from 'react';
import {Platform, TouchableOpacity, Text, View} from 'react-native';
import BackgroundJob from 'react-native-background-job';
import axios from "axios";

const backgroundJob = {
  jobKey: "myJob",
  job: async () => {
    try{
      let res = await axios.get("https://jsonplaceholder.typicode.com/posts/42")
      console.log(res.data)
    }catch(e){
      console.log(e);
    }

  }
 };

BackgroundJob.register(backgroundJob);

export default function App(){

  const startJob = () => {
    BackgroundJob.schedule({
      jobKey: "myJob",
      period: 5000,
      exact: true,
      allowExecutionInForeground: true,
    });
  }

  return (
    <View style={{flex:1}}>
      <TouchableOpacity onPress={startJob}>
        <Text>Start</Text>
      </TouchableOpacity>
    </View>
  );
}

yes its working when application running in background/foreground . but when application is killed or closed its not working ..