BackgroundTask

Call a nanoflow when app is in background based on the BackgroundFetch API for Android and iOS.

Features

Background Fetch is a very simple plugin which attempts to awaken an app in the background about every 15 minutes, providing a short period of background running-time.

This widget is build to incorporate the https://github.com/transistorsoft/react-native-background-fetch into Mendix Native apps

iOS

  • There is no way to increase the rate which a fetch-event occurs and this plugin sets the rate to the most frequent possible — you will never receive an event faster than 15 minutes. The operating-system will automatically throttle the rate the background-fetch events occur based upon usage patterns. Eg: if user hasn't turned on their phone for a long period of time, fetch events will occur less frequently.
  • scheduleTask seems only to fire when the device is plugged into power.
  • ⚠️ When your app is terminated, iOS no longer fires events — There is no such thing as stopOnTerminate: false for iOS.
  • iOS can take days before Apple's machine-learning algorithm settles in and begins regularly firing events. Do not sit staring at your logs waiting for an event to fire. If your simulated events work, that's all you need to know that everything is correctly configured.
  • If the user doesn't open your iOS app for long periods of time, iOS will stop firing events.

Android

Usage

  • Clone this Git
  • Go to cloned directory
  • Run: npm i
  • Run: cd ios && pod install
  • Follow steps below:

iOS

Select the root of your project. Select Capabilities tab. Enable Background Modes and enable the following mode:

  • Background fetch
68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f3976696b356b786f6b6c6b36336f622f696f732d73657475702d6261636b67726f756e642d6d6f6465732e706e673f646c3d31

Configure Info.plist (iOS 13+)

Open your Info.plist and add the key "Permitted background task scheduler identifiers" 68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f7435786667616832676768717477732f696f732d73657475702d7065726d69747465642d6964656e746966696572732e706e673f646c3d31

Add the required identifier com.transistorsoft.fetch.

68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f6b7764696f327272323536643835322f696f732d73657475702d7065726d69747465642d6964656e746966696572732d6164642e706e673f646c3d31

AppDelegate.m (iOS 13+)

#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

+//IMPORTANT:  Paste import ABOVE the DEBUG macro
+#import <TSBackgroundFetch/TSBackgroundFetch.h>

#if DEBUG
.
. ///////////////////////////////////////////////////////////////////////////////////
. // IMPORTANT:  DO NOT paste import within DEBUG macro or archiving will fail!!!
. ///////////////////////////////////////////////////////////////////////////////////
.
#endif

@implementation AppDelegate

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  .
  .
  .
+//[REQUIRED] Register BackgroundFetch
+[[TSBackgroundFetch sharedInstance] didFinishLaunching];

  return YES;
}

Android

android/build.gradle

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }
+       maven {
+           // react-native-background-fetch
+           url("${project(':react-native-background-fetch').projectDir}/libs")
+       }

    }
}

Configure proguard-rules.pro

If you're using minifyEnabled true with your Android release build, the plugin's HeadlessTask class will be mistakenly removed and you will have this crash.

  1. Edit android/app/proguard-rules.pro.
  2. Add the following rule:
# [react-native-background-fetch]
-keep class com.transistorsoft.rnbackgroundfetch.HeadlessTask { *; }

Debug Xcode command:

e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.transistorsoft.fetch"]

Issues, suggestions and feature requests

https://github.com/IncentroBA/backgroundTask/issues

Development and contribution

Description
Call a nanoflow when app is in background using the BackgroundFetch API on Android and iOS
Readme 242 KiB
Languages
TypeScript 94.9%
JavaScript 5.1%