Expo BackgroundFetch
一个提供用于执行后台获取任务的 API 的库。
已弃用:expo-background-fetch
库正在被expo-background-task
中的新版本取代。expo-background-fetch
未收到补丁,将在即将发布的版本中移除。
expo-background-fetch
提供了执行 后台获取 任务的 API,允许你在后台定期运行特定代码来更新你的应用。该模块在底层使用 TaskManager Native API。
¥expo-background-fetch
provides an API to perform background fetch tasks, allowing you to run specific code periodically in the background to update your app. This module uses TaskManager Native API under the hood.
已知的问题
¥Known issues
BackgroundFetch
仅在应用处于后台时有效,而在应用终止或设备重新启动时无效。你可以查看 相关的 GitHub 问题 了解更多详细信息。
¥BackgroundFetch
only works when the app is backgrounded, not if the app was terminated or upon device reboot. You can check out the relevant GitHub issue for more details.
在 iOS 上,BackgroundFetch
库要求你使用 开发构建,因为 iOS Expo Go 应用中未启用后台获取。
¥On iOS the BackgroundFetch
library requires you to use a development build since Background Fetch is not enabled in the iOS Expo Go app.
安装
¥Installation
-
npx expo install expo-background-fetch
If you are installing this in an existing React Native app, make sure to install expo
in your project.
配置
¥Configuration
为了能够在 iOS 上运行后台获取任务,你需要将 fetch
值添加到应用的 Info.plist 文件中的 UIBackgroundModes
数组中。这是后台获取正常工作所必需的。
¥To be able to run background fetch tasks on iOS, you need to add the fetch
value to the UIBackgroundModes
array in your app's Info.plist file. This is required for background fetch to work properly.
如果你使用 CNG,所需的 UIBackgroundModes
配置将由预构建自动应用。
¥If you're using CNG, the required UIBackgroundModes
configuration will be applied automatically by prebuild.
Configure UIBackgroundModes manually on iOS
If you're not using Continuous Native Generation (CNG) or you're using a native ios project manually, then you'll need to add the following to your Expo.plist file:
!!!IG0!!!
Usage
Below is an example that demonstrates how to use expo-background-fetch
.
!!!IG1!!!
Triggering background fetches
Background fetches can be difficult to test because they can happen inconsistently. Fortunately, you can trigger background fetches manually when developing your apps.
For iOS, you can use the Instruments
app on macOS to manually trigger background fetches:
- Open the Instruments app. The Instruments app can be searched through Spotlight (⌘ + Space) or opened from
/Applications/Xcode.app/Contents/Applications/Instruments.app
- Select
Time Profiler
- Select your device / simulator and pick the
Expo Go
app - Press the
Record
button in the top left corner - Navigate to the
Document
Menu and selectSimulate Background Fetch - Expo Go
:
For Android, you can set the minimumInterval
option of your task to a small number and background your application like so:
!!!IG2!!!
API
!!!IG3!!!
Methods
Deprecated Use
getStatusAsync()
fromexpo-background-task
instead. Theexpo-background-fetch
package has been deprecated.
Gets a status of background fetch.
Promise<BackgroundFetchStatus | null>
Returns a promise which fulfils with one of BackgroundFetchStatus
enum values.
Deprecated Use
registerTaskAsync()
fromexpo-background-task
instead. Theexpo-background-fetch
package has been deprecated.
Parameter | Type | Description |
---|---|---|
taskName | string | Name of the task to register. The task needs to be defined first - see |
options(optional) | BackgroundFetchOptions | An object containing the background fetch options. Default: {} |
Registers background fetch task with given name. Registered tasks are saved in persistent storage and restored once the app is initialized.
Promise<void>
Example
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
TaskManager.defineTask(YOUR_TASK_NAME, () => {
try {
const receivedNewData = // do your background fetch here
return receivedNewData ? BackgroundFetch.BackgroundFetchResult.NewData : BackgroundFetch.BackgroundFetchResult.NoData;
} catch (error) {
return BackgroundFetch.BackgroundFetchResult.Failed;
}
});
Deprecated Use the
registerTaskAsync()
method from expo-background-task package, and specifyBackgroundTaskOptions
argument instead, when setting task interval time.
Parameter | Type | Description |
---|---|---|
minimumInterval | number | Number of seconds that must elapse before another background fetch can be called. |
Sets the minimum number of seconds that must elapse before another background fetch can be initiated. This value is advisory only and does not indicate the exact amount of time expected between fetch operations.
This method doesn't take any effect on Android. It is a global value which means that it can overwrite settings from another application opened through Expo Go.
Promise<void>
A promise which fulfils once the minimum interval is set.
Deprecated Use
unregisterTaskAsync()
fromexpo-background-task
instead. Theexpo-background-fetch
package has been deprecated.
Parameter | Type | Description |
---|---|---|
taskName | string | Name of the task to unregister. |
Unregisters background fetch task, so the application will no longer be executing this task.
Promise<void>
A promise which fulfils when the task is fully unregistered.
Interfaces
Property | Type | Description |
---|---|---|
minimumInterval(optional) | number | Inexact interval in seconds between subsequent repeats of the background fetch alarm. The final interval may differ from the specified one to minimize wakeups and battery usage.
|
startOnBoot(optional) | boolean | Only for: Android Whether to restart background fetch events when the device has finished booting. Default: false |
stopOnTerminate(optional) | boolean | Only for: Android Whether to stop receiving background fetch events after user terminates the app. Default: true |
Enums
This return value is to let iOS know what the result of your background fetch was, so the platform can better schedule future background fetches. Also, your app has up to 30 seconds to perform the task, otherwise your app will be terminated and future background fetches may be delayed.
BackgroundFetchStatus.Denied = 1
The user explicitly disabled background behavior for this app or for the whole system.
BackgroundFetchStatus.Restricted = 2
Background updates are unavailable and the user cannot enable them again. This status can occur when, for example, parental controls are in effect for the current user.
Permissions
Android
On Android, this module might listen when the device is starting up. It's necessary to continue working on tasks started with startOnBoot
. It also keeps devices "awake" that are going idle and asleep fast, to improve reliability of the tasks. Because of this both the RECEIVE_BOOT_COMPLETED
and WAKE_LOCK
permissions are added automatically.
Android Permission | Description |
---|---|
Allows an application to receive the Intent.ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.
| |
Allows using PowerManager WakeLocks to keep processor from sleeping or screen from dimming. |