Update LICENSE, README.md, and 13 more files...
This commit is contained in:
60
src/BackgroundTask.tsx
Normal file
60
src/BackgroundTask.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Component, ReactNode, createElement } from "react";
|
||||
import { View } from "react-native";
|
||||
import BackgroundFetch from "react-native-background-fetch";
|
||||
|
||||
import { Style } from "@mendix/pluggable-widgets-tools";
|
||||
|
||||
import { BackgroundTaskProps } from "../typings/BackgroundTaskProps";
|
||||
|
||||
export interface CustomStyle extends Style {
|
||||
}
|
||||
|
||||
export class BackgroundTask extends Component<BackgroundTaskProps<CustomStyle>> {
|
||||
constructor(props: BackgroundTaskProps<CustomStyle>){
|
||||
super(props)
|
||||
}
|
||||
|
||||
render(): ReactNode {
|
||||
return (
|
||||
//Render an empty view so widget can be used in Mendix modeler.
|
||||
<View></View>
|
||||
);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
// Initialize BackgroundFetch ONLY ONCE when component mounts.
|
||||
this.initBackgroundFetch();
|
||||
}
|
||||
|
||||
async initBackgroundFetch() {
|
||||
console.log('Configuring BackgroundFetch')
|
||||
// BackgroundFetch event handler.
|
||||
const onEvent = async (taskId: string | undefined) => {
|
||||
console.log('[BackgroundFetch] task: ', taskId);
|
||||
// Execute nanoflow
|
||||
this.props.backgroundFlow?.execute();
|
||||
// IMPORTANT: You must signal to the OS that your task is complete.
|
||||
BackgroundFetch.finish(taskId)
|
||||
}
|
||||
|
||||
// Timeout callback is executed when your Task has exceeded its allowed running-time.
|
||||
// You must stop what you're doing immediately BackgorundFetch.finish(taskId) or else fetch will never be executed again by OS.
|
||||
const onTimeout = async (taskId: any) => {
|
||||
console.warn('[BackgroundFetch] TIMEOUT task: ', taskId);
|
||||
BackgroundFetch.finish(taskId)
|
||||
}
|
||||
|
||||
// Initialize BackgroundFetch only once when component mounts.
|
||||
await BackgroundFetch.configure(
|
||||
{
|
||||
minimumFetchInterval: 30, //Minimum interval time for schedule. Can be (much) longer.
|
||||
enableHeadless: true, //HeadlessJS mode for Android only
|
||||
startOnBoot: true, //Start when device starts, Android only
|
||||
stopOnTerminate: false //Stop task when app is killed, Android only
|
||||
},
|
||||
onEvent,
|
||||
onTimeout,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
17
src/BackgroundTask.xml
Normal file
17
src/BackgroundTask.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<widget id="incentro.backgroundtask.BackgroundTask" pluginWidget="true" needsEntityContext="true" offlineCapable="true"
|
||||
supportedPlatform="Native"
|
||||
xmlns="http://www.mendix.com/widget/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.mendix.com/widget/1.0/ ../node_modules/mendix/custom_widget.xsd">
|
||||
<name>Background Task</name>
|
||||
<description>Call a nanoflow when app is in background using the BackgroundFetch API on Android and iOS</description>
|
||||
<icon/>
|
||||
<properties>
|
||||
<propertyGroup caption="Background Task">
|
||||
<property key="backgroundFlow" type="action" required="false">
|
||||
<caption>Backgroundtask</caption>
|
||||
<description>Action to perform in the background (only nanoflow is allowed, no opening pages etc.)</description>
|
||||
</property>
|
||||
</propertyGroup>
|
||||
</properties>
|
||||
</widget>
|
||||
11
src/package.xml
Normal file
11
src/package.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<package xmlns="http://www.mendix.com/package/1.0/">
|
||||
<clientModule name="BackgroundTask" version="1.0.0" xmlns="http://www.mendix.com/clientModule/1.0/">
|
||||
<widgetFiles>
|
||||
<widgetFile path="BackgroundTask.xml"/>
|
||||
</widgetFiles>
|
||||
<files>
|
||||
<file path="incentro/backgroundtask"/>
|
||||
</files>
|
||||
</clientModule>
|
||||
</package>
|
||||
Reference in New Issue
Block a user