Rapsssito / react-native-background-actions

React Native background service library for running background tasks forever in Android & iOS.
MIT License
759 stars 102 forks source link

In android, this is not working #192

Closed pankaj-stan closed 9 months ago

pankaj-stan commented 11 months ago

when run service its not working .

arifrohmanhakim commented 11 months ago

just console

LOG Trying to start background service LOG Successful start!

ameerhamzaWAL commented 11 months ago

not working on android 13 version. SDK 33

MalhotraKunal commented 11 months ago

When I run BackgroundJob.start() It logs ----> Started Succesfully But when i run BackgroundJob.isRunning() it logs ----> false

AmirBraham commented 10 months ago

any updates ?

Gareeeb7 commented 10 months ago

Try to use below code for BG task-----------------------------------

function JA_BackgroundAction(taskName, taskTitle, taskDesc, linkingURI, runOnce, repeatInterval, stopOnExit, action, notificationColor) { BackgroundService.on('expiration', () => { console.log('iOS: I am being closed!'); });

console.log('Setting time');
const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));

const taskRunOnce = async () => {
  if (Platform.OS === 'ios') {
    console.warn(
      'This task will not keep your app alive in the background by itself, use other library like react-native-track-player that use audio,',
      'geolocalization, etc. to keep your app alive in the background while you excute the JS from this library.'
    );
  }
  await new Promise(async (_resolve) => {
    // running my action
  });
};

const taskRepeat = async (taskData) => {
  console.log('BG Runing.....................................................')
  if (Platform.OS === 'ios') {
    console.warn(
      'This task will not keep your app alive in the background by itself, use other library like react-native-track-player that use audio,',
      'geolocalization, etc. to keep your app alive in the background while you excute the JS from this library.'
    );
  }
  await new Promise(async (_resolve) => {
    const { delay } = taskData;
    for (let i = 0; BackgroundService.isRunning(); i++) {

      // write your background task here.----------------------------------------------------------------------

      await sleep(delay);
    }
  });
};

var options = {
  taskName: taskName,
  taskTitle: taskTitle,
  taskDesc: taskDesc,
  taskIcon: {
    name: 'app_logo',
    type: 'drawable',
  },
  color: notificationColor,
  linkingURI: linkingURI,
  parameters: {
    delay: 40000,
  },
};

// function handleOpenURL(evt) {
//   console.log(evt.url);
//   // do something with the url
// }

// Linking.addEventListener('url', handleOpenURL);

const initiateBackgroundTask = async () => {

  console.log('taskName: ' + options.taskName);
  console.log('taskTitle: ' + options.taskTitle);
  console.log('taskDesc: ' + options.taskDesc);
  console.log('linkingURI: ' + options.linkingURI);
  console.log('color: ' + options.color);

  if (!runOnce) {
    options.parameters.delay = repeatInterval;
  }

  try {
    console.log('Trying to start background service');
    var taskName = options.taskName;

    if (runOnce) {
      await BackgroundService.start(taskRunOnce, options);
      console.log('Running process with identifier : "' + taskName + '".');
      console.log('Successful start!');
    }
    else {
      if (!BackgroundService.isRunning()) {
        await BackgroundService.start(taskRepeat, options);
        console.log('Running process with identifier : "' + taskName + '".');
        console.log('Successful start!');
      }
      else {
        console.log('Start failed. Another process already running.');
      }
    }
  } catch (e) {
    console.log('Error', e);
  }
};

const stopBackgroundTask = async () => {
  console.log('Stop background service');
  await BackgroundService.stop();
};

console.log('Initiating bg');

console.log('Initiating task ..');

initiateBackgroundTask();

}

AmirBraham commented 10 months ago

@Gareeeb7 Thanks I'll try and see if it works . I'm mainly using it for android

YashP2711 commented 10 months ago

@AmirBraham does it work on your end?

AmirBraham commented 10 months ago

@YashP2711 I'm currently facing a bug , I'm using react-native-wallpaper manager to change the wallpaper in the background and it causes some crashes. So I'm not really sure if this code works or not .

zerojuan commented 10 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>
YashP2711 commented 10 months ago

@zerojuan I did this step, but no success

tygovip127 commented 10 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>

It's work for me. Thank you

lesleysin commented 10 months ago

This issue fixed in https://github.com/Rapsssito/react-native-background-actions/pull/184 But it not released yet

humblesami commented 10 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>

Yes that's been solution for me too

Asim-Abuhasanain commented 10 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>

Worked for me! Thank you.

Luminted commented 9 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>

Worked for me! Thank you.

This has solved my issue. Thanks!

kindapath commented 9 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>

Solved my issue and helped me a lot, thanks, buddy!

johnson-KF commented 9 months ago

I think the service is not automatically added, so you'll need to add it in your AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    ...
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
   ...
  </application>
</manifest>

This works for me

Rapsssito commented 9 months ago

This should be fixed in version 3.0.1.