transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

Headless task not running on physical device emulator with Android version 10 #484

Closed asrasoft-admin closed 2 weeks ago

asrasoft-admin commented 3 months ago

Your Environment

Expected Behavior

I expected the headless task registered using Background.registerHeadlessTask to run on my physical device emulator but it does work fine on the emulator with Android version 14.

Actual Behavior

The headless task runs successfully on the emulator with Android version 14, but it does not work on my physical device emulator with Android version 10. I have attempted to manually run the task using the command adb shell cmd jobscheduler run -f com.myapplicationId 999 on my physical device, but it did not trigger the headless task.

Steps to Reproduce

  1. Install the app on the physical device emulator with Android version 10.
  2. Manually run the background task using the command adb shell cmd jobscheduler run -f com.myapplicationId 999.
  3. Check if the headless task is triggered.
  4. Compare the behavior with the emulator running Android version 14.

Context

I am attempting to use the Background.registerHeadlessTask method to register a headless task in my React Native application. The task runs successfully on the emulator with Android version 14, but it fails to run on my physical device emulator with Android version 10. This issue is critical for the functionality of my application as it relies on background tasks for important functionality.

Debug logs

Emulator (Android 14)

02-10 18:37:48.150 9758 9813 D TSBackgroundFetch: - finish: com.transistorsoft.customtask 02-10 18:37:48.152 9758 9813 D TSBackgroundFetch: - FetchAlarmReceiver finish 02-10 18:38:05.661 9758 9758 D TSBackgroundFetch: ☯️ HeadlessMode? true 02-10 18:38:11.221 9921 9921 D TSBackgroundFetch: - Background Fetch event received: react-native-background-fetch 02-10 18:38:11.222 9921 9921 D TSBackgroundFetch: ☯️ onCreate 02-10 18:38:11.274 9921 9921 D TSBackgroundFetch: ☯️ HeadlessMode? true 02-10 18:38:11.752 9921 9962 D TSBackgroundFetch: [RNBackgroundFetch initialize] 02-10 18:38:12.305 9921 9921 D TSBackgroundFetch: onHeadlessJsTaskStart: 1 02-10 18:38:13.466 9921 9965 I ReactNativeJS: 'This is the headless event: ', '2/10/2024, 6:38:13 PM', { timeout: false, taskId: 'react-native-background-fetch' } 02-10 18:38:13.473 9921 9965 E ReactNativeJS: TypeError: Cannot read property 'then' of undefined, js engine: hermes 02-10 18:38:13.491 9921 9966 D TSBackgroundFetch: - finish: react-native-background-fetch 02-10 18:38:13.491 9921 9966 D TSBackgroundFetch: - jobFinished 02-10 18:38:13.509 9921 9965 I ReactNativeJS: Channel created: false

Physical Device Emulator (Android 10)

02-10 18:05:31.623 20509 20509 D TSBackgroundFetch: ☯️ onStart 02-10 18:05:32.873 20509 20509 D TSBackgroundFetch: ☯️ onResume 02-10 18:05:42.793 20509 20509 D TSBackgroundFetch: - Background Fetch event received: react-native-background-fetch 02-10 18:05:42.798 20509 20604 I ReactNativeJS: '[BackgroundFetch] Task started', 'react-native-background-fetch' 02-10 18:05:42.800 20509 20604 I ReactNativeJS: '<=========> Syncing Started At', Sat Feb 10 2024 18:05:42 GMT+0500, undefined, '<=========>' 02-10 18:05:42.803 20509 20604 I ReactNativeJS: No Data to Sync 02-10 18:05:42.806 20509 20605 D TSBackgroundFetch: - finish: react-native-background-fetch 02-10 18:05:42.806 20509 20605 D TSBackgroundFetch: - jobFinished 02-10 18:05:47.492 20509 20509 D TSBackgroundFetch: ☯️ onPause 02-10 18:05:47.493 20509 20509 D TSBackgroundFetch: ☯️ onStop

christocracy commented 3 months ago

'This is the headless event: ', '2/10/2024, 6:38:13 PM', { timeout: false, taskId: 'react-native-background-fetch' } 02-10 18:38:13.473 9921 9965 E ReactNativeJS: TypeError: Cannot read property 'then' of undefined, js engine: hermes

asrasoft-admin commented 3 months ago

'This is the headless event: ', '2/10/2024, 6:38:13 PM', { timeout: false, taskId: 'react-native-background-fetch' } 02-10 18:38:13.473 9921 9965 E ReactNativeJS: TypeError: Cannot read property 'then' of undefined, js engine: hermes

Yeah, but the logs which you have quoted are the logs of my android studio emulator (on which it is working fine, just added for your reference only). This log is shown after when the headless task runs perfectly using the adb command. The issue occurs when i try to run it on my physical device emulator, i have also tested it with multiple devices (Android v10, v11) but the headless task is not running using the command. Even after testing the apk on phone it shows the same result (not running).

asrasoft-admin commented 3 months ago

This is my index.js:

PushNotification.configure({
  onRegister: function (token) {
    console.log('TOKEN:', token);
  },

  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification);

    if (Platform.OS === 'ios')
      notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  onAction: function (notification) {
    console.log('ACTION:', notification.action);
    console.log('NOTIFICATION:', notification);
  },

  onRegistrationError: function (err) {
    console.error(err.message, err);
  },

  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  popInitialNotification: true,

  requestPermissions: Platform.OS === 'ios',
});

PushNotification.createChannel(
  {
    channelId: 'channel-id',
    channelName: 'Channel Name',
    channelDescription: 'A brief description of the channel',
    soundName: 'default',
    importance: 4,
    vibrate: true,
  },
  created => console.log(`Channel created: ${created}`),
);

const Root = () => {
  return (
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <App />
      </PersistGate>
    </Provider>
  );
};

AppRegistry.registerComponent(appName, () => Root);

const backgroundHeadlessTask = event => {
  if (event.timeout) {
    console.log('[BackgroundFetch] 💀 HeadlessTask TIMEOUT: ', event.taskId);
    BackgroundFetch.finish(event.taskId);
    return;
  }

  console.log(
    'This is the headless event: ',
    new Date().toLocaleString(),
    event,
  );

  PushNotification.localNotification({
    channelId: 'channel-id',
    title: 'Background Task Completed ',
    message:
      'Your headless task has been triggered with task id ' + event.taskId,
  });

  BackgroundFetch.finish(event.taskId);
};

BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask);
christocracy commented 3 months ago

Please learn to syntax highlight fenced code blocks

asrasoft-admin commented 3 months ago

Okay I have edited my comment.

christocracy commented 3 months ago

I have no issue receiving simulated background-fetch headless-tasks on my Google Pixel (Android 10):

02-13 15:16:40.399 11955 11955 D TSBackgroundFetch: - Background Fetch event received: react-native-background-fetch
02-13 15:16:40.401 11955 11955 D TSBackgroundFetch: onHeadlessJsTaskStart: 23
02-13 15:16:40.402 11955 11986 I ReactNativeJS: '[BackgroundFetch HeadlessTask] start', 'react-native-background-fetch'
02-13 15:16:40.719 11955 11986 I ReactNativeJS: [BackgroundFetch HeadlessTask] finished
02-13 15:16:40.720 11955 11987 D TSBackgroundFetch: - finish: react-native-background-fetch
02-13 15:16:40.720 11955 11987 D TSBackgroundFetch: - jobFinished
github-actions[bot] commented 4 weeks ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been inactive for 14 days since being marked as stale.