Rapsssito / react-native-background-actions

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

In android, this is not working #192

Closed pankaj-stan closed 1 year ago

pankaj-stan commented 1 year ago

when run service its not working .

arifrohmanhakim commented 1 year ago

just console

LOG Trying to start background service LOG Successful start!

ameerhamzaWAL commented 1 year ago

not working on android 13 version. SDK 33

MalhotraKunal commented 1 year ago

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

AmirBraham commented 1 year ago

any updates ?

Gareeeb7 commented 1 year 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 1 year ago

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

YashP2711 commented 1 year ago

@AmirBraham does it work on your end?

AmirBraham commented 1 year 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 1 year 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 1 year ago

@zerojuan I did this step, but no success

phu-quoc commented 1 year 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 1 year ago

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

humblesami commented 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago

This should be fixed in version 3.0.1.