$ yarn add react-native-background-fetch
$ npm install --save react-native-background-fetch
>= 1.10+
:
$ pod --version
// if < 1.10.0
$ sudo gem install cocoapods
$ cd ios
$ pod install
-
Select the root of your project. Select Capabilities tab. Enable Background Modes and enable the following mode:
-
Background fetch
-
Background processing (:new: iOS 13+; Only if you intend to use
BackgroundFetch.scheduleTask
)
- Open your
Info.plist
and add the key "Permitted background task scheduler identifiers"
- Add the required identifier
com.transistorsoft.fetch
.
- If you intend to execute your own custom tasks via
BackgroundFetch.scheduleTask
, you must add those custom identifiers as well. For example, if you intend to execute a customtaskId: 'com.transistorsoft.customtask'
, you must add the identifiercom.transistorsoft.customtask
to your "Permitted background task scheduler identifiers", as well.
com.transistorsoft.
— In the future, the com.transistorsoft
prefix may become required.
BackgroundFetch.scheduleTask({
taskId: 'com.transistorsoft.customtask',
delay: 60 * 60 * 1000 // In one hour (milliseconds)
});
The BGTaskScheduler
API introduced in iOS 13 requires special setup:
#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
// IMPORTANT: Paste import ABOVE the DEBUG macro
+#import <TSBackgroundFetch/TSBackgroundFetch.h>
#if DEBUG
.
. ///////////////////////////////////////////////////////////////////////////////////
. // IMPORTANT: DO NOT paste import within DEBUG macro or archiving will fail!!!
. ///////////////////////////////////////////////////////////////////////////////////
.
#endif
@implementation AppDelegate
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
.
.
.
+ // [REQUIRED] Register BackgroundFetch
+ [[TSBackgroundFetch sharedInstance] didFinishLaunching];
return YES;
}
< iOS 13
.
BackgroundFetch implements an AppDelegate
method didPerformFetchWithCompletionHandler
. You must manually add this file to the same folder where your AppDelegate.m
lives:
- In the XCode's
Project navigator
, right click on project's name ➜Add Files to <...>
. node_modules/react-native-background-fetch/ios/RNBackgroundFetch/RNBackgroundFetch+AppDelegate.m
.
node_modules/react-native-background-fetch/ios/RNBackgroundFetch/RNBackgroundFetch+AppDelegate.m