I've implemented react-native-background-fetch in my app, and it works well on Android, but not on ios - Xcode Version 11.4
Simulate Background Fetch works. I noticed also that Background Fetch triggers once on the first application start, but not anymore. On Android Background Fetch triggers every 15 min.
My configuration is:
react-native: 0.62.1
react-native-background-fetch: 2.8.0
Here is my code:
App.js
class App extends Component { componentDidMount () { ... newPostNotificationService.initNotifications() ...
newPostsNotificationSevice.js
initNotifications = async () => { this.configureNotifications() // Configures PushNotification BackgroundFetch.registerHeadlessTask(this.fetchNewPosts) BackgroundFetch.configure({ minimumFetchInterval: 15, stopOnTerminate: false, startOnBoot: true, enableHeadless: true }, this.fetchNewPosts, (error) => { console.log(error, '[js] RNBackgroundFetch failed to start') }) BackgroundFetch.status((status) => { switch (status) { case BackgroundFetch.STATUS_RESTRICTED: console.log('BackgroundFetch restricted') break case BackgroundFetch.STATUS_DENIED: console.log('BackgroundFetch denied') break case BackgroundFetch.STATUS_AVAILABLE: console.log('BackgroundFetch is enabled') break } })}
fetchNewPosts Method in newPostsNotificationSevice.js
async fetchNewPosts(taskId) { console.log('BackgroundFetch Start', taskId) if (AppState.currentState === 'active') { BackgroundFetch.finish(taskId) return } dataStorageService.getSettings().then(async (settings) => { if (!settings.notifications) { BackgroundFetch.finish(taskId) return } const url = config.API_URL + config.API_ENDPOINTS.POSTS +'&per_page=1&page=1' let data if (!config.DATA_MOCK.NEW_POSTS) { data = await fetch(url) .then((response) => { return response.json() }) .catch((error) => { console.error(error) }) } else { data = postsMock.posts } data = data.map(item => { return new PostShort(item) }) data = data.filter(item => { return item.sendPush }) if (data.length === 0) { BackgroundFetch.finish(taskId) } dataStorageService.getPostsIndex().then(postsIndex => { if (postsIndex.length > 0 && postsIndex.indexOf(data[0].id) !== -1) { BackgroundFetch.finish(taskId) return } dataStorageService.getNotificationsIndex().then(notificationsIndex => { if (notificationsIndex.indexOf(data[0].id) !== -1) { BackgroundFetch.finish(taskId) return } const notificationBody = entities.decode(data[0].title) const notificationNumber = notificationsIndex.length + 1 console.log('notificationNumber', notificationNumber) PushNotification.localNotification({ title: config.NOTIFICATION_TITLE, // (optional) message: notificationBody, // (required) playSound: true, // (optional) default: true soundName: 'default', number: notificationNumber, id: notificationNumber.toString() }) dataStorageService.insertIntoNotificationsIndex([data[0].id]) BackgroundFetch.finish(taskId) }) }) })}
I made iOS setup following this instructions: https://github.com/transistorsoft/react-native-background-fetch/blob/HEAD/docs/INSTALL-AUTO-IOS.md
Why Background Fetch doesn't trigger periodically on iOS like on Android does?