react-native-boundary: Headless event listener not called when app is killed

Hey @eddieowens !

When the app is in foreground or background the boundary callbacks work perfectly! But I am struggling with the Headless part when the app is killed. In logcat I can see that the event is fired. I see the log message from boundary’s index.js file:

ReactNativeJS: 'onExit', [ 'rated0' ]

which comes from this part of code here:

const HeadlessBoundaryEventTask = async ({event, ids}) => {
  console.log(event, ids);
  boundaryEventEmitter.emit(event, ids)
};

But my callback is never executed. I think the issue might be timing. When the app starts because of a Headless event I can see that boundary receives the event as said above. I added log messages for the Callback Registration and it seems to happen after the Headless event:

15:03:19.837 ReactNativeJS: 'onExit', [ 'rated0' ] // from boundary's index.js
15:03:19.863 ReactNativeJS: Registering Boundary Callbacks // my code

I moved the callback registration to the very first line of my index.js but the order still seems to be incorrect. Any idea how to solve this? Where should I call the Boundary.on callback registration in my code?

Setup:

  • “react-native”: “0.59.5”
  • “react-native-activity-recognition”: “woffu/react-native-boundary” // fix for newer android versions

Help is very much appreciated 😃

About this issue

Most upvoted comments

@NuclearKev I am working on a fix for iOS, should come very shortly.

@SufniDroid just like how react-native-boundary does it:

const HeadlessBoundaryEventTask = async ({event, ids}) => {
  console.log(event, ids);
  boundaryEventEmitter.emit(event, ids)
};

So in our case:

const HeadlessBoundaryEventTask = async ({event, ids}) => {
     if(event === "onEnter"){
        //my code here
     }
};

EDIT: or like this if you don’t want to have a named function:

AppRegistry.registerHeadlessTask('OnBoundaryEvent', async ({event, ids}) => {
    if(event === "onEnter"){
        //my code here
     }
})

@mathiasmoeller I was having the same issue and fixed it by registering another headless task in index.js for the event. Then I just have it run the same code as it normally would. This method doesn’t work for iOS, however, so it’s not a great solution. I can’t get iOS to do stuff when the app is killed and I don’t know why.