Skip to content

Commit 57e2691

Browse files
joshstrangeihadeed
authored andcommitted
feat(background-fetch): Adding Background Fetch requested in #990 (#1013)
* Adding Background Fetch (https://github.com/transistorsoft/cordova-plugin-background-fetch) requested in #990 * Adding BackgroundFetch to index
1 parent 09a7dcf commit 57e2691

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { AppRate } from './plugins/apprate';
1313
import { AppUpdate } from './plugins/app-update';
1414
import { AppVersion } from './plugins/appversion';
1515
import { Badge } from './plugins/badge';
16+
import { BackgroundFetch } from './plugins/background-fetch';
1617
import { BackgroundGeolocation } from './plugins/background-geolocation';
1718
import { BackgroundMode } from './plugins/backgroundmode';
1819
import { Backlight } from './plugins/backlight';
@@ -140,6 +141,7 @@ export * from './plugins/appodeal';
140141
export * from './plugins/apprate';
141142
export * from './plugins/app-update';
142143
export * from './plugins/appversion';
144+
export * from './plugins/background-fetch';
143145
export * from './plugins/background-geolocation';
144146
export * from './plugins/backgroundmode';
145147
export * from './plugins/backlight';
@@ -272,6 +274,7 @@ window['IonicNative'] = {
272274
AppVersion,
273275
Badge,
274276
BackgroundGeolocation,
277+
BackgroundFetch,
275278
BackgroundMode,
276279
Backlight,
277280
BarcodeScanner,

src/plugins/background-fetch.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)