For context: I'm developing an alarm app, which needs to play a sound at a certain time, even when the app is not running/not in the foreground. Preferably, it should wake up the screen and open the alarm page where the user can shut it off (but at this point I'd be happy if it rings or does anything at all)
The issue: Expo/React Native stops all activities by default once it's not running in the foreground anymore
What I tried (a lot, but here are the 'solutions' where I felt, it's closest to the answer):
- Using the built in feature with expo Audio
staysActiveInBackground, which works only if I exit the app when the sound already plays (so the other way around) react-native-foreground-service-androidlibrary: https://www.npmjs.com/package/react-native-foreground-service-android which basically creates a notification channel to keep the background tasks running (In my case it would keep the setTimeout running until it triggers the sound. I think that would be the answer, if I would only use a react native without expo. There should be a way around it, but I'm kind of lost, because there are barely any resources out there that explain how to use native libraries within an expo managed app- This 'hack': https://dev.to/chafikgharbi/a-trick-to-play-sound-on-screen-lock-or-background-in-react-native-and-expo-32ml it uses Expo's
WebView, but I cannot get it to work with setTimeout + the stop sound function doesn't execute correctly (here the code that I tried on snack: https://snack.expo.dev/@janaabfl/webview)
This is the code that should be running in the background (at the time of the alarm, the first code leads to another page where the second code runs on render):
// Alarm page
setTimeout(function activateAlarm() {
navigation.push('AlarmRings');
}, ringsInMilliseconds);
// AlarmRings page
useEffect(() => {
playSound();
}, []);
and here's a link to a small version of my project to replicate the issue: https://github.com/janaabf/wakeup-buddy
I appreciate ANY help, I've been stuck on this for way too long and ejecting expo is not an option.