Javascript [react native][ios][react native background fetch]dont';我不能在ios上工作
我已经在我的应用程序中实现了react native background fetch,它在Android上运行良好,但在ios上运行不好-Xcode 11.4版 模拟后台获取工作。我还注意到后台提取在第一次启动应用程序时触发一次,但现在不再触发了。在Android后台,每15分钟触发一次Fetch 我的配置是: 反应本机:0.62.1 react本机后台提取:2.8.0 这是我的密码: App.jsJavascript [react native][ios][react native background fetch]dont';我不能在ios上工作,javascript,ios,react-native,react-native-background-fetch,Javascript,Ios,React Native,React Native Background Fetch,我已经在我的应用程序中实现了react native background fetch,它在Android上运行良好,但在ios上运行不好-Xcode 11.4版 模拟后台获取工作。我还注意到后台提取在第一次启动应用程序时触发一次,但现在不再触发了。在Android后台,每15分钟触发一次Fetch 我的配置是: 反应本机:0.62.1 react本机后台提取:2.8.0 这是我的密码: App.js class App extends Component { componentDidMou
class App extends Component {
componentDidMount () {
...
newPostNotificationService.initNotifications()
...
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
}
})
}
newPostsNotificationSevice.js
class App extends Component {
componentDidMount () {
...
newPostNotificationService.initNotifications()
...
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
}
})
}
fetchNewPostsnewPostsNotificationSevice.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设置:
为什么后台抓取不会像Android那样在iOS上定期触发?好消息:)最后,它起作用了。两天后,iPhone上出现了通知。iOS后台提取算法是有线的。这意味着当模拟后台提取在Xcode中工作时,它应该在一段时间后自动触发。我们只能设置最小频率-15分钟。最大频率是多少?这是个谜。