2

我已经在我的应用程序中实现了 react-native-background-fetch,它在 Android 上运行良好,但在 ios 上运行良好 - Xcode 版本 11.4

模拟后台获取工作。我还注意到 Background Fetch 在第一次应用程序启动时触发一次,但不再触发。在 Android 后台提取每 15 分钟触发一次。

我的配置是:

反应原生:0.62.1

反应原生背景获取:2.8.0

这是我的代码:

应用程序.js

class App extends Component {
  componentDidMount () {
    ...
    newPostNotificationService.initNotifications()
    ...

newPostsNotificationService.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
    }
  })
}

newPostsNotificationService.js中的 fetchNewPosts 方法

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

为什么后台获取不会像在 Android 上那样在 iOS 上定期触发?

4

0 回答 0