The dark side of Background tasks in React Native - React Alicante 2018
In this talk we are going to explore how to manage background tasks within your React Native application. Background tasks are essentially tasks that run when the app is not in the foreground (visible).
Audio, AirPlay and Picture in Picture Location updates Newsstand downloads External accessory communication Use Bluetooth LE accessories Act as Bluetooth LE accessory Background fetch Remote notifications
Setup Headless JS - Native public class SyncShowsService extends HeadlessJsTaskService { @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) { return new HeadlessJsTaskConfig( "SyncShowsTask", null, // We could pass arguments 120000, // Timeout in ms false // Task is allowed in foreground ); } }
When to start it? “ Now, whenever you start your service, e.g. as a periodic task or in response to some system event / broadcast, JS will spin up, run your task, then spin down. “
Android @ReactMethod public void init(int syncInterval, int syncFlexTime) { // All Java code described in Android docs } @Override public void onPerformSync(...) { Intent service = new Intent( getContext(), HeadlessService.class ); getContext().startService(service); }