0

I'm having a few issues with the background fetch library. Everything works well when I tested it in Android 6, the task was running while the app was in the background, when it was terminated and when the phone rebooted, as it should. But then when I tried it on Android 10 it worked while being in the background and after being terminated but after rebooting the phone i get nothing.

Maybe there is some additional step that i have to do to set up the background fetch properly on newer versions of Android.

Environment

I did a expo eject, expo diagnostics returns the following:

Expo CLI 3.22.0 environment info:
    System:
      OS: Windows 10 10.0.19041
    Binaries:
      Node: 14.5.0 - C:\Program Files\nodejs\node.EXE
      Yarn: 1.22.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
      npm: 6.14.5 - C:\Program Files\nodejs\npm.CMD
    IDEs:
      Android Studio: Version  3.6.0.0 AI-192.7142.36.36.6392135
    npmPackages:
      expo: ~38.0.1 => 38.0.8
      react: ~16.11.0 => 16.11.0
      react-dom: ~16.11.0 => 16.11.0
      react-native: ~0.62.2 => 0.62.2
      react-native-web: ~0.11.7 => 0.11.7

Reproducible Demo

App.js

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import PushNotificaiton from "react-native-push-notification"
import * as BackgroundFetch from 'expo-background-fetch'
import * as TaskManager from 'expo-task-manager'

const TASK_NAME = "task"

TaskManager.defineTask(TASK_NAME, () => {
  PushNotificaiton.localNotification({
    title: "Background fetch test",
    message: "Time: " + new Date().toUTCString()
  })
})

export default function App() {

  return (
    <View style={styles.container}>
      <Text>Open up App.js to start working on your app!</Text>
      <Button title="Show notification test" onPress={() => 
        PushNotificaiton.localNotification({
          title: "It works",
          message: "Time: " + new Date().toUTCString()
        })}/>
      <Button title="Register background task" onPress={async () =>  {
        await BackgroundFetch.registerTaskAsync(TASK_NAME, { minimumInterval: parseInt(60), stopOnTerminate: false, startOnBoot: true });
        console.log(`task registered, minimumInterval: ${60}`);  
      }}/>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

You can get the demo project from here: https://github.com/samedb/expo-background-fetch-test Whenever the background task gets executed we should get a notification with the current time. The task executes every minute.

Steps to Reproduce

I created a release apk to test this, idk maybe there was a better way to do this.

  • run the app and click 'Register background task'
  • try if it works while the app is in the background (it should)
  • try if it works when the app is terminated (it should)
  • try if it works when you reboot the device without manually running the app (that is the problem)

As I said it work on Android 6 but it does not work on Android 10, i have not tested it on the versions in between.

Expected Behavior vs Actual Behavior

Expected behavior: the background task should start working by itself after the reboot Actual behavior: it doesn't start by itself, you have to run the app manually for the task to start

kenmistry
  • 1,934
  • 1
  • 15
  • 24
Samed Bejtovic
  • 301
  • 2
  • 8

1 Answers1

0

I found the solution to this problem, it has nothing to do with expo-background-fetch. On newer versions you have to go to the phone settings and give apps permission to work in the background, start after reboot and so on. On some phones its called unmonitored app on others you have to go to the battery settings and set up the "App launch" settings. I guess on the Android 6 phone that we tested this it was already enabled by default, on newer version we had to enable it manually in the settings.

Samed Bejtovic
  • 301
  • 2
  • 8