Background Fetch
iOS后臺獲取實現(xiàn)偏陪。 請參閱: https://developer.apple.com/reference/uikit/uiapplication#1657399 iOS后臺獲取是一個基本的API,它每15分鐘(在用戶黃金時段)喚醒您的應(yīng)用程序铡恕,并為您的應(yīng)用程序提供30秒 的后臺運行時間。 當后臺獲取事件發(fā)生時丢间,此插件將執(zhí)行您提供的callbackFn探熔。 沒有辦法提高獲取事件發(fā)生的速率,此插件將速率設(shè)置為UIApplicationBackgroundFetchIntervalMinimum的最常見的可能值 - iOS會根據(jù)設(shè)備使用情況和時間自動確定速率(即:fetch- 在黃金時段時間約為15分鐘烘挫,假設(shè)睡眠時間較短诀艰,例如上午3點)。(簡而言之就是app在打開app之前就進行后臺獲取數(shù)據(jù)) 有關(guān)詳細信息饮六,請參閱 https://github.com/transistorsoft/cordova-plugin-background-fetch
Repo(備用): https://github.com/transistorsoft/cordova-plugin-background-fetch
Installation(安裝)
1.安裝Cordova和Ionic插件:
$ ionic cordova plugin add cordova-plugin-background-fetch
$ npm install --save @ionic-native/background-fetch
Supported platforms(支持平臺)
iOS
Usage(用法)
import { BackgroundFetch, BackgroundFetchConfig } from '@ionic-native/background-fetch';
constructor(private backgroundFetch: BackgroundFetch) {
const config: BackgroundFetchConfig = {
stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true.
};
backgroundFetch.configure(config)
.then(() => {
console.log('Background Fetch initialized');
this.backgroundFetch.finish();
})
.catch(e => console.log('Error initializing background fetch', e));
// Start the background-fetch API. Your callbackFn provided to #configure will be executed each time a background-fetch event occurs. NOTE the #configure method automatically calls #start. You do not have to call this method after you #configure the plugin
backgroundFetch.start();
// Stop the background-fetch API from firing fetch events. Your callbackFn provided to #configure will no longer be executed.
backgroundFetch.stop();
}
Instance Members(實例成員)
配置插件的fetch callbackFn
參數(shù) | 類型 | 詳情 |
---|---|---|
config | BackgroundFetchConfig | 插件配置 |
****Returns:** Promise<any>
啟動background-fetch API其垄。 每次發(fā)生后臺獲取事件時,#configure提供的CallbackFn將被執(zhí)行卤橄。 注意#configure方法自動調(diào)用start绿满。 配置插件后,您不必調(diào)用此方法
****Returns:** Promise<any>
停止后臺獲取API從觸發(fā)獲取事件窟扑。 您提供給#configure的callbackFn將不再執(zhí)行喇颁。
****Returns:** Promise<any>
您必須在提交給#configure的fetch callbackFn中調(diào)用此方法,以向iOS發(fā)出您的擷取操作已完成辜膝。 iOS只提供30秒的后臺時間來獲取事件 - 如果超過這30秒无牵,iOS會殺死你的應(yīng)用程序漾肮。
status()
返回后臺獲取的狀態(tài)
****Returns:** Promise<any>
參數(shù) | 類型 | 詳情 |
---|
設(shè)置true以在用戶關(guān)閉應(yīng)用程序后停止后臺運行厂抖。 默認為true。(可選)