react-native-background-fetch: iOS: background-fatch not working
I’m working on background fetch for the local notification function with time intervals of 15 minutes, but this is not working. function does not work after 15-20 minutes.
Your Environment
- Platform: iOS
- React Native version (
react-native -v): 0.74.1 - React-native-background-fetch: “^4.2.5”,
AppDelegate.m
#import "AppDelegate.h"
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import <Firebase.h>
#import <React/RCTBundleURLProvider.h>
#import <TSBackgroundFetch/TSBackgroundFetch.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleName = @"Disaster Counter Measures";
// You can add your custom initial props in the dictionary below.
// They will be passed down to the ViewController used by React Native.
self.initialProps = @{};
// Define UNUserNotificationCenter
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// [REQUIRED] Register BackgroundFetch
[[TSBackgroundFetch sharedInstance] didFinishLaunching];
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
}
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
info.plist
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.transistorsoft.fetch</string>
<string>com.transistorsoft.customtask</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
</array>
code
// index.js
const configureBackgroundFetch = () => {
BackgroundFetch.configure(
{
minimumFetchInterval: 15,
stopOnTerminate: false,
startOnBoot: true,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
requiresCharging: false,
requiresDeviceIdle: false,
requiresBatteryNotLow: false,
requiresStorageNotLow: false,
},
async (taskId) => {
console.log('[BackgroundFetch] Event received: ', taskId);
showlocalNotification() //it will show local notification
BackgroundFetch.finish(taskId);
},
(error) => {
console.error('[BackgroundFetch] failed to start: ', error);
handleError({
error,
screen_name: 'Index',
function_name: 'configureBackgroundFetch',
});
},
);
};
// Call configureBackgroundFetch on app load
configureBackgroundFetch();
In my project, I have also set up for https://github.com/transistorsoft/react-native-background-geolocation,
And if I log states of react-native-background-fetch configuration, then it gives me status code 2
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 20 (11 by maintainers)
That’s correct. iOS uses a machine-learning model to determine the rate that events are fired. This model would presumably use, among other things, the frequency and length of time the user brings your app to the foreground as a strong signal for how often to fire events.
It’s completely up to the OS when tasks run.
iOS does not run tasks when the app is terminated. For Android, see api docs in the readme
enableHeadlessandstopOnTerminate@christocracy , is background fetch can runs also in the foreground ?
second question, i saw that BG task can also execute if app is closed (terminated) how can i test it in xcode because when closing the app, xcode dev session is terminated ?
thanks.
The plug-in is not at all involved with firing events — it’s completely up to the OS.
you simulate events to confirm that everything is configure correctly. That is all you need to do.
See the readme to learn how to simulate events. When simulated events work, your job is done.
it can take DAYS, as I’ve been saying here for 10 years, for the OS to begin firing events regularly. Just bring your app to foreground periodically to train the machine learning model. Do not terminate the app.
@christocracy Hi, this is my block of code and im running it in useEffect after login to app. when goes to background the event not fired even not after 15 minutes. tested on xcode with wired device. all configured correctly i think with appDelegate.m
scheduleTaskmethod is a relatively recent addition.The traditional purpose of this plugin is execute your callback function provided to
.configureaccording to the configuredminimumFetchIntervalThat’s all you need to do.
.scheduleTaskis an optional feature which is not required to simply have a periodic task to run.That’s exactly what the readme says, with a big ⚠️ icon.