Skip to content

Commit b9d206b

Browse files
committed
Implemented background task
1 parent b419fb2 commit b9d206b

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Example project using [react-native-background-task](https://github.com/jamesisaac/react-native-background-task) to run periodic background tasks in a simple React Native app.
2+
3+
## Installation
4+
5+
Clone the repo and run `npm install`

index.js

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,90 @@
1-
/**
2-
* Sample React Native App
3-
* https://github.com/facebook/react-native
4-
* @flow
5-
*/
1+
// @flow
62

73
import React, { Component } from 'react';
84
import {
5+
Alert,
96
AppRegistry,
7+
AsyncStorage,
8+
Button,
9+
ScrollView,
1010
StyleSheet,
1111
Text,
1212
View
1313
} from 'react-native';
14+
import BackgroundTask from 'react-native-background-task'
15+
16+
function currentTimestamp(): string {
17+
const d = new Date()
18+
const z = n => n.toString().length == 1 ? `0${n}` : n // Zero pad
19+
return `${d.getFullYear()}-${z(d.getMonth()+1)}-${z(d.getDate())} ${z(d.getHours())}:${z(d.getMinutes())}`
20+
}
21+
22+
BackgroundTask.define(
23+
async () => {
24+
console.log('Hello from a background task')
25+
26+
const value = await AsyncStorage.getItem('@MySuperStore:times')
27+
await AsyncStorage.setItem('@MySuperStore:times', `${value || ''}\n${currentTimestamp()}`)
28+
29+
// Or, instead of just setting a timestamp, do an http request
30+
/* const response = await fetch('http://worldclockapi.com/api/json/utc/now')
31+
const text = await response.text()
32+
await AsyncStorage.setItem('@MySuperStore:times', text) */
33+
34+
BackgroundTask.finish()
35+
},
36+
)
1437

1538
export default class AsyncStorageTimestamp extends Component {
39+
constructor() {
40+
super()
41+
this.state = {
42+
text: '',
43+
}
44+
}
45+
46+
componentDidMount() {
47+
BackgroundTask.schedule()
48+
this.checkStatus()
49+
}
50+
51+
async checkStatus() {
52+
const status = await BackgroundTask.statusAsync()
53+
console.log(status.available)
54+
}
55+
1656
render() {
1757
return (
18-
<View style={styles.container}>
58+
<ScrollView style={styles.container}>
1959
<Text style={styles.welcome}>
20-
Welcome to React Native!
60+
react-native-background-task!
2161
</Text>
2262
<Text style={styles.instructions}>
23-
To get started, edit index.android.js
63+
The defined task (storing the current timestamp in AsyncStorage)
64+
should run while the app is in the background every 15 minutes.
2465
</Text>
2566
<Text style={styles.instructions}>
26-
Double tap R on your keyboard to reload,{'\n'}
27-
Shake or press menu button for dev menu
67+
Press the button below to see the current value in storage.
2868
</Text>
29-
</View>
69+
<Button
70+
onPress={async () => {
71+
const value = await AsyncStorage.getItem('@MySuperStore:times')
72+
console.log('value', value)
73+
this.setState({ text: value })
74+
}}
75+
title="Get"
76+
/>
77+
<Text>{this.state.text}</Text>
78+
</ScrollView>
3079
);
3180
}
3281
}
3382

3483
const styles = StyleSheet.create({
3584
container: {
3685
flex: 1,
37-
justifyContent: 'center',
38-
alignItems: 'center',
3986
backgroundColor: '#F5FCFF',
87+
padding: 15,
4088
},
4189
welcome: {
4290
fontSize: 20,

0 commit comments

Comments
 (0)