Я реализовал act-native-background-fetch в своем приложении, и он хорошо работает на Android, но не на ios - Xcode Version 11.4
Имитация фоновой выборки работает. Я также заметил, что Background Fetch срабатывает один раз при первом запуске приложения, но не больше. Вкл Android Background Fetch срабатывает каждые 15 мин.
Моя конфигурация:
реакция-нативная: 0,62,1
реакция-нативная-фон-выборка: 2,8,0
Вот мой код :
Приложение. 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 Метод в 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)
})
})
})
}
Я сделал iOS настройку, следуя этой инструкции: https://github.com/transistorsoft/react-native-background-fetch/blob/HEAD/docs/INSTALL-AUTO-IOS.md
Почему фоновая выборка не ' t срабатывает периодически на iOS как на Android делает?