transistorsoft/react-native-background-fetch

How register headless task in expo SDK 50

Closed this issue · 8 comments

Your Environment

  • Plugin version: 4.2.4
  • Platform: android
  • OS version: any
  • Device manufacturer / model: any
  • React Native version (react-native -v): 0.73.6

Expected Behavior

Use this plugin with expo project with headless mode on.

Actual Behavior

Missing documentation on how to use headless mode in expo.

This is question, as plugin works perfectly with react native CLI (tested on most androids) even when app is terminated.
Expo SDK50 doesn't have index.js file, so I don't know where to put this code:

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

const backgroundFetchHeadlessTask = async (event) => {
  await Event.create...
  BackgroundFetch.finish(event.taskId);
}

/// Now register the handler.
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);

Since in expo project there is no AppRegistry.registerComponent.
It could be good idea to see somewhere in documentation how to integrate plugin in expo.
I mean it is easy to install it but adding this code for index file is missing.

📂 package.json:

{
+  "main": "index.js"
}
  • Create new file index.js:
import registerRootComponent from 'expo/build/launch/registerRootComponent';

import App from './App';

registerRootComponent(App);

// Add your HeadlessTask here
//
//

I’ll give it a try. I didn’t have idea it could be done like this in expo.

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

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

I am having "main": "expo-router/entry" this in my package.json file. Also I am using expo tabs template which has _layout.tsx things. So how can I implement that headless task?

Screenshot 2024-08-12 at 3 31 47 PM Screenshot 2024-08-12 at 3 32 33 PM

My file structure is like this templete and it doesn't have any appEntry.js or index.js. I want to register firebase messaging as a headless task so that i can receive notification even when my app is dead.

index.tsx is the Typescript equivalent of an index.js file.