Rapsssito / react-native-background-actions

React Native background service library for running background tasks forever in Android & iOS.
MIT License
818 stars 117 forks source link

Cannot read property 'start' of undefined #203

Closed sitannsu closed 4 months ago

sitannsu commented 11 months ago
await BackgroundService.start(veryIntensiveTask, options);  it throws  Cannot read property 'start' of undefined 
Omare123 commented 10 months ago

I've been fighting this all day too.

`import { StatusBar } from 'expo-status-bar'; import { Platform, StyleSheet, Text, View } from 'react-native'; import { Header } from './src/Components/Header'; import { useEffect, useState, useRef} from 'react'; import React from 'react'; import { PomodoroContainer } from './src/Components/PomodoroContainer'; import { PomodoroButton } from './src/Components/PomodoroButton'; import { Timer } from './src/Components/Timer'; import as Notifications from 'expo-notifications'; import as Device from 'expo-device'; import BackgroundService from 'react-native-background-actions';

const FOCUS_TIME_MINUTES = 0.1 60 1000; const BREAK_TIME_MINUTES = 0.05 60 1000; const LONG_BREAK_TIME_MINUTES = 0.1 60 1000; const BREAK_TIMES = 3; const FOCUS = "Focus"; const BREAK = "Break"

const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(null), time));

const veryIntensiveTask = async (taskDataArguments) => { // Example of an infinite loop task const { delay } = taskDataArguments; await new Promise(async (resolve) => { for (let i = 0; BackgroundService.isRunning(); i++) { console.log(i); await BackgroundService.updateNotification({taskDesc: Running...${i}}); await sleep(delay); } }); };

const options = { taskName: 'Example', taskTitle: 'ExampleTask title', taskDesc: 'ExampleTask description', taskIcon: { name: 'ic_launcher', type: 'mipmap', }, color: '#ff00ff', linkingURI: 'pomodoro://chat/jane', // See Deep Linking for more info parameters: { delay: 1000, }, }; export default function App() { const [timerCount, setTimerCount] = useState(FOCUS_TIME_MINUTES); const [timerIntervalId, setTimerIntervalId] = useState<NodeJS.Timeout | null>(null); const [breakTimerCount, setBreakTimerCount] = useState(BREAK_TIMES); const [timerMode, setTimerMode] = useState<"Focus" | "Break">(FOCUS); const timerDate = new Date(timerCount);

const startBackgroundService = async () => { await BackgroundService.start(veryIntensiveTask, options); await BackgroundService.updateNotification({taskDesc: 'Running...'}); } const stopBackgroundService = async () => { await BackgroundService.stop(); } const startTimer = async () =>{ let id = setInterval(() => setTimerCount(prev => prev - 1000), 1000); setTimerIntervalId(id); }

function stopTimer(){ if(timerIntervalId !== null){ clearInterval(timerIntervalId); setTimerIntervalId(null); } }

function resetTimer(){ if(timerIntervalId !== null){ stopTimer(); } setTimerCount(FOCUS_TIME_MINUTES); setTimerMode(FOCUS); }

return (

{timerMode === FOCUS ? "Time to focus!" : "Time for a break!"}

); }

const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', flexDirection: 'column', alignContent: 'center', alignItems: 'center', justifyContent: 'center', minHeight: '10%', width: '100%' } }); `

mr2726 commented 9 months ago

Did you find a solution?

MarkNaArea commented 8 months ago

PR #208 worked for me

Rapsssito commented 4 months ago

Duplicate of #208