我正在开发一个带有电容的离子应用程序。我希望我的应用程序能够在终止后执行后台任务。如果我正确地阅读了文档,通过设置
stopOnTerminate: false我已经创建了一个空白的应用程序,我可以测试backgroundFetch插件,并使用以下代码。
const config: BackgroundFetchConfig = {
stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true.
};
this.backgroundFetch.configure(config)
.then(() => {
console.log('Background Fetch initialized');
this.backgroundFetch.finish();
})
.catch(e => console.log('Error initializing background fetch', e));
this.backgroundFetch.start();但是,当我构建应用程序并使用电容打开它时,我可以在android studio logcat窗口中看到它没有更新stopOnTerminate值。
com.example.app D/TSBackgroundFetch: {
"taskId": "cordova-background-fetch",
"isFetchTask": true,
"minimumFetchInterval": 15,
"stopOnTerminate": true, // This should have changed to false
"requiredNetworkType": 0,
"requiresBatteryNotLow": false,
"requiresCharging": false,
"requiresDeviceIdle": false,
"requiresStorageNotLow": false,
"startOnBoot": false,
"forceAlarmManager": false,
"periodic": true,
"delay": -1
}我还需要做些什么才能改变缺省值吗?
发布于 2020-03-05 11:22:11
使用导入BackgroundFetch插件的不同方式设法使其工作
import BackgroundFetch from 'cordova-plugin-background-fetch';然后,在配置选项中,我需要添加enableHeadless以使其正常工作。
// Your background-fetch handler.
const fetchCallback = (taskId) => {
console.log('[js] BackgroundFetch event received: ', taskId);
// Required: Signal completion of your task to native code
// If you fail to do this, the OS can terminate your app
// or assign battery-blame for consuming too much background-time
BackgroundFetch.finish(taskId);
};
const failureCallback = (error) => {
console.log('- BackgroundFetch failed', error);
};
BackgroundFetch.configure(fetchCallback, failureCallback, {
minimumFetchInterval: 15, // <-- default is 15
stopOnTerminate: false,
enableHeadless: true // <-- Added this line
});配置现在正在android studio中被更改。
https://stackoverflow.com/questions/60531475
复制相似问题