Closed pankaj-stan closed 1 year ago
just console
LOG Trying to start background service LOG Successful start!
not working on android 13 version. SDK 33
When I run BackgroundJob.start() It logs ----> Started Succesfully But when i run BackgroundJob.isRunning() it logs ----> false
any updates ?
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();
}
@Gareeeb7 Thanks I'll try and see if it works . I'm mainly using it for android
@AmirBraham does it work on your end?
@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 .
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>
@zerojuan I did this step, but no success
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
This issue fixed in https://github.com/Rapsssito/react-native-background-actions/pull/184 But it not released yet
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
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.
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!
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!
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
This should be fixed in version 3.0.1.
when run service its not working .