Skip to main content

Quick start guide

This guide will walk you through the basic setup and usage of React Native Background Guardian to ensure your app can run reliably in the background on Android devices.
1

Install the library

Add React Native Background Guardian to your project:
For iOS projects, install pods:
2

Import and use wake locks

Import the library and use wake locks to keep the CPU running during background tasks:
The wake lock timeout defaults to 24 hours (86,400,000 ms). Always release the wake lock when your work is complete to preserve battery life.
3

Check battery optimization status

Check if your app is exempt from battery optimizations and request exemption if needed:
Google Play restricts the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission. Only use it if your app genuinely requires background execution (messaging, health tracking, device management, etc.).
4

Handle OEM-specific settings

Many Android manufacturers have additional battery optimization settings. Guide users to these settings for devices with aggressive battery management:
The library supports OEM settings for Xiaomi, Samsung, Huawei, Honor, OnePlus, Oppo, Vivo, Realme, Asus, and more. For unsupported manufacturers, it falls back to standard battery optimization settings.
5

Monitor power save mode

Check if Battery Saver mode is active, as it affects all apps regardless of exemption status:
Power Save mode is different from battery optimization exemptions. An app can be exempt from Doze mode but still be affected by Power Save mode restrictions.

Complete example

Here’s a complete example using a React hook to manage background guardian functionality:

Common use cases

Background audio player

Foreground kiosk mode

Next steps

API Reference

Explore all available methods and their parameters

Android Doze Mode

Learn how to survive Android’s battery optimization features