|
| 1 | +import {Cordova, Plugin} from './plugin'; |
| 2 | + |
| 3 | +declare var window; |
| 4 | + |
| 5 | + |
| 6 | +export interface BackgroundFetchConfig { |
| 7 | + |
| 8 | + /** |
| 9 | + * Set true to cease background-fetch from operating after user "closes" the app. Defaults to true. |
| 10 | + */ |
| 11 | + stopOnTerminate?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * @name BackgroundFetch |
| 17 | + * @description |
| 18 | + * iOS Background Fetch Implementation. See: https://developer.apple.com/reference/uikit/uiapplication#1657399 |
| 19 | + * iOS Background Fetch is basically an API which wakes up your app about every 15 minutes (during the user's prime-time hours) and provides your app exactly 30s of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs. There is no way to increase the rate which a fetch-event occurs and this plugin sets the rate to the most frequent possible value of UIApplicationBackgroundFetchIntervalMinimum -- iOS determines the rate automatically based upon device usage and time-of-day (ie: fetch-rate is about ~15min during prime-time hours; less frequently when the user is presumed to be sleeping, at 3am for example). |
| 20 | + * For more detail, please see https://github.com/transistorsoft/cordova-plugin-background-fetch |
| 21 | + * |
| 22 | + * @usage |
| 23 | + * |
| 24 | + * ```typescript |
| 25 | + * import { BackgroundFetch } from 'ionic-native'; |
| 26 | + * |
| 27 | + * |
| 28 | + * // When device is ready : |
| 29 | + * platform.ready().then(() => { |
| 30 | + * |
| 31 | + * let config = { |
| 32 | + * stopOnTerminate: false, // Set true to cease background-fetch from operating after user "closes" the app. Defaults to true. |
| 33 | + * }; |
| 34 | + * |
| 35 | + * BackgroundFetch.configure(() => { |
| 36 | + console.log('[js] BackgroundFetch initiated'); |
| 37 | +
|
| 38 | + // perform some ajax request to server here |
| 39 | +
|
| 40 | + You MUST called #finish so that native-side can signal completion of the background-thread to the os. |
| 41 | + BackgroundFetch.finish(); |
| 42 | +
|
| 43 | + * }, (error) => { |
| 44 | + * console.log('- BackgroundFetch failed', error); |
| 45 | + * }, config); |
| 46 | + * |
| 47 | + * }); |
| 48 | + * |
| 49 | + * // 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 |
| 50 | + * BackgroundFetch.start(); |
| 51 | + * |
| 52 | + * // Stop the background-fetch API from firing fetch events. Your callbackFn provided to #configure will no longer be executed. |
| 53 | + * BackgroundFetch.stop(); |
| 54 | + * |
| 55 | + * ``` |
| 56 | + * @interfaces |
| 57 | + * BackgroundFetchConfig |
| 58 | + * |
| 59 | + */ |
| 60 | +@Plugin({ |
| 61 | + pluginName: 'BackgroundFetch', |
| 62 | + plugin: 'cordova-plugin-background-fetch', |
| 63 | + pluginRef: 'BackgroundFetch', |
| 64 | + repo: 'https://github.com/transistorsoft/cordova-plugin-background-fetch', |
| 65 | + platforms: ['iOS'] |
| 66 | +}) |
| 67 | +export class BackgroundFetch { |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * Configures the plugin's fetch callbackFn |
| 72 | + * |
| 73 | + * @param {Function} callbackFn This callback will fire each time an iOS background-fetch event occurs (typically every 15 min). |
| 74 | + * @param {Function} errorCallback The failureFn will be called if the device doesn't support background-fetch. |
| 75 | + * @param {BackgroundFetchConfig} config Configuration for plugin |
| 76 | + * @return Location object, which tries to mimic w3c Coordinates interface. |
| 77 | + * See http://dev.w3.org/geo/api/spec-source.html#coordinates_interface |
| 78 | + * Callback to be executed every time a geolocation is recorded in the background. |
| 79 | + */ |
| 80 | + @Cordova({ |
| 81 | + sync: true |
| 82 | + }) |
| 83 | + static configure(callbackFn: Function, errorCallback: Function, config: BackgroundFetchConfig): any { return; } |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + /** |
| 88 | + * Start the background-fetch API. |
| 89 | + * 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 |
| 90 | + * @returns {Promise<any>} |
| 91 | + */ |
| 92 | + @Cordova() |
| 93 | + static start(): Promise<any> { return; } |
| 94 | + |
| 95 | + /** |
| 96 | + * Stop the background-fetch API from firing fetch events. Your callbackFn provided to #configure will no longer be executed. |
| 97 | + * @returns {Promise<any>} |
| 98 | + */ |
| 99 | + @Cordova() |
| 100 | + static stop(): Promise<any> { return; } |
| 101 | + |
| 102 | + /** |
| 103 | + * You MUST call this method in your fetch callbackFn provided to #configure in order to signal to iOS that your fetch action is complete. iOS provides only 30s of background-time for a fetch-event -- if you exceed this 30s, iOS will kill your app. |
| 104 | + */ |
| 105 | + @Cordova() |
| 106 | + static finish() { } |
| 107 | +} |
0 commit comments