-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
87 lines (75 loc) · 2.41 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { Component } from 'react';
import { StyleSheet, Text, View, Button, AppState } from 'react-native';
import PushController from './src/PushController';
import PushNotification from 'react-native-push-notification';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {};
this.handleAppStateChange = this.handleAppStateChange.bind(this);
this.sendNotification = this.sendNotification.bind(this);
};
componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);
};
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
};
// This will notify the user in 3 seconds after sending the app to the
// background (like after pressing the home button or switching apps)
handleAppStateChange(appState) {
console.log(appState);
if (appState === 'background') {
PushNotification.localNotificationSchedule({
color: "#DE5F43",
title: "AppState::background",
message: 'Since background::Scheduled::done!', // (required)
date: new Date(Date.now() + (3 * 1000)) // in 3 secs
});
}
else if(appState === 'active') {
PushNotification.localNotification({
title: "componentDidMount",
message: 'componentDidMount::done!',
color: "#DE5F43"
});
}
};
sendNotification() {
PushNotification.localNotification({
color: "#DE5F43", // (optional) default: system default
id: '123',
title: "Button Notification", // (optional)
message: 'sendNotification::done!'
});
// Schedule a notification
PushNotification.localNotificationSchedule({
title: 'Button Scheduled Notification',
message: 'Button Scheduled delay notification message 5sec::done!', // (required)
color: "#DE5F43",
date: new Date(Date.now() + (5 * 1000)) // in 3 secs
});
};
render() {
return (
<View style={styles.container}>
<PushController />
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Button title='Press here for a notification' onPress={this.sendNotification} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});