Does the app need to stay in the foreground?

No. You can even close the app and it will reappear on wakeup time.