react-native-background-fetch
react-native-background-fetch copied to clipboard
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