我目前正在使用 expo sdk-version 32 为iOS开发一个应用程序。我必须从我的应用程序定期致电REST服务。为此,我尝试使用新的BackgroundFetch API。
这是我的代码:
const BACKGROUND_LOCATION_SENDING_TASK = "BACKGROUND_LOCATION_SENDING_TASK";
async function initBackgroundLocationSending() {
console.log("initBackgroundLocationSending()");
TaskManager.defineTask(BACKGROUND_LOCATION_SENDING_TASK, () => {
// this console.log does never get called ...
console.log("BACKGROUND_LOCATION_SENDING_TASK");
// i will implement real fetch logic here when i found out how to get this function called.
return BackgroundFetch.Result.NewData;
});
console.log("is task registered ... ");
let isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("isRegistered: ", isRegistered);
if(isRegistered) {
console.log("unregister task ...");
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("Done");
}
console.log("is task registered ... ");
isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("isRegistered: ", isRegistered);
console.log("register task ...");
await BackgroundFetch.registerTaskAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("OK");
console.log("is task registered ... ");
isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("isRegistered: ", isRegistered);
console.log("set minimum interval ...");
await BackgroundFetch.setMinimumIntervalAsync(60);
console.log("OK");
console.log("get status ... ");
const status = await BackgroundFetch.getStatusAsync();
console.log("status: ", status);
}
从 app.js 中调用函数的控制台输出:
: initBackgroundLocationSending()
: is task registered ...
: isRegistered: true
: unregister task ...
: OK
: is task registered ...
: isRegistered: false
: register task ...
: OK
: is task registered ...
: isRegistered: true
: set minimum interval ...
: OK
: get status ...
: status: 3
来自Expo的背景文档,我了解状态:3 表示BackgroundFetch.Status.Available
。
ios 我的 app.json 的部分:
...
"ios": {
"supportsTablet": true,
"bundleIdentifier": "xxx-yyy-zzz",
"infoPlist": {
"UIBackgroundModes": [
"location",
"fetch"
],
"NSLocationWhenInUseUsageDescription": "... (replaced)",
"NSLocationAlwaysAndWhenInUseUsageDescription": "... (replaced)",
"NSLocationAlwaysUsageDescription": "... (replaced)"
}
},
...
我的测试设备是带有iOS 12.1.3的iPhone 6s。
我缺少什么?为什么甚至几分钟后都没有运行任务?
btw:我使用Expo的背景位置而没有任何问题
我会尝试通过将任务定义移动到文档状态,因为您当前设置它的方式在函数范围内定义
- 通过提供一个名称和应执行的函数来定义任务:这需要在全局范围中调用(例如在您的反应组件之外)
意味着这个
const BACKGROUND_LOCATION_SENDING_TASK = "BACKGROUND_LOCATION_SENDING_TASK";
async function initBackgroundLocationSending() {
console.log("initBackgroundLocationSending()");
TaskManager.defineTask(BACKGROUND_LOCATION_SENDING_TASK, () => {
// this console.log does never get called ...
console.log("BACKGROUND_LOCATION_SENDING_TASK");
// i will implement real fetch logic here when i found out how to get this function called.
return BackgroundFetch.Result.NewData;
});
....
}
应该更改为此
const BACKGROUND_LOCATION_SENDING_TASK = "BACKGROUND_LOCATION_SENDING_TASK";
TaskManager.defineTask(BACKGROUND_LOCATION_SENDING_TASK, () => {
// this console.log does never get called ...
console.log("BACKGROUND_LOCATION_SENDING_TASK");
// i will implement real fetch logic here when i found out how to get this function called.
return BackgroundFetch.Result.NewData;
});
async function initBackgroundLocationSending() {
console.log("initBackgroundLocationSending()");
...
}
处理任务注册的initBackgroundLocationSending
的其余部分应在组件安装